Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent dupe calls #545

Closed
wants to merge 2 commits into from
Closed

Conversation

tlambert03
Copy link
Member

@tlambert03 tlambert03 commented Mar 11, 2023

trying to fix the duplicate call thing mentioned in #544 (review)

@tlambert03
Copy link
Member Author

ugh... this is just a real annoying problem.

  1. in order to truly support the proper Optional[type] return annotation that @Czaki was going for in Allow to use Optional annotation in function return type for magicgui functions napari/napari#5595, magicgui needs to move from a simple type -> list[callback] map, to a much more complicated set[type] -> list[callback] map... and then when looking up a callback for a return type annotation, look for the first set[type] that is a superset of the return callback set.
  2. that more complicated map makes everything more complicated:
    • it slows down callback lookup at runtime
    • it makes it much harder to undo type registration (e.g. in the type_registered context manager)

basically, this all just further emphasizes that the return_callback feature of register_type is far more complicated to do "correctly", and makes me want to deprecate the whole feature altogether as mentioned in #543 ...

I really don't know the best solution here.

@tlambert03
Copy link
Member Author

one possibility here is just to temporarily go back to the previous behavior (that I absolutely thing was incorrect behavior) of calling callbacks registered for Type even when the return annotation of the @magicgui function is Optional[Type] (as mentioned in #541).

That's a big bummer, cause it's just obviously wrong behavior. @Czaki, what do you think here?

@Czaki
Copy link
Contributor

Czaki commented Mar 12, 2023

Why not just check presence of callback in lust before append? Registration type should be rare operation and such list should be short.

@tlambert03
Copy link
Member Author

Because if we're dealing with sets as keys you now have to check all keys for a subset match, remember which ones matched, etc...

@tlambert03
Copy link
Member Author

It's not that it's not possible, it's just getting uglier and uglier for something that was an accidental "feature/bug" to begin with

@Czaki
Copy link
Contributor

Czaki commented Mar 27, 2023

Because of type2callback return first callback list where superset is found this test will fail:

def test_proper_call():
    @magicgui
    def func(a: int, b: int) -> int:
        return a + b

    mock1 = Mock()
    mock2 = Mock()

    register_type(int, return_callback=mock1)
    register_type(Optional[int], return_callback=mock2)

    func(1, 2)

    mock2.assert_called_once()
    mock1.assert_called_once()

@tlambert03
Copy link
Member Author

closing, see #546

@tlambert03 tlambert03 closed this Aug 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants