-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed
Labels
bugmypy got something wrongmypy got something wrongpriority-0-hightopic-pluginsThe plugin API and ideas for new pluginsThe plugin API and ideas for new plugins
Description
(Taken from #4869)
When certain dunder methods are called implicitly through language syntax, the plugin function/method hooks are called incorrectly or not at all. Specifically, the following cases cause problems:
- For some dunder methods (
__setitem__,__iter__,__next__),get_function_hookis incorrectly called instead ofget_method_hook, and the passed in method name is not fully qualified -
get_method_signature_hookis not called for some dunder methods (__getitem__,__setitem__,__iter__,__next__) - When a
Type[...]expression is called (that is not a simple class name), the class name passed toget_function_hookis not fully qualified (this is not strictly related to dunder methods, it might be a separate problem, I'm not sure) (fixed in Support classes and type aliases in plugin hooks #5379)
To test this, I've added prints to mypy.plugin.DefaultPlugin's methods get_function_hook, get_method_signature_hook, get_method_hook, and get_attribute_hook to show when each method is called with what argument. Then I ran mypy on the following code:
# fixed
# * class name is not fully qualified ('list') (fixed in #5379)
seq = (list,)[0](("hello",))
# works
# * class name is fully qualified ('builtins.list')
seq = list(("hi",))
# buggy
# * name is not fully qualified ('__iter__ of list')
# * get_function_hook is called instead of get_method_hook
# * get_method_signature_hook is not called
for obj in seq:
print(obj)
# buggy
# * get_method_signature_hook is not called
print(seq[0])
# buggy
# * name is not fully qualified ('__setitem__ of list')
# * get_function_hook is called instead of get_method_hook
# * get_method_signature_hook is not called
seq[0] = "bye"
# works
# * name is fully qualified ('builtins.list.clear')
# * get_method_signature_hook is called
seq.clear()Output:
get_function_hook('builtins.list')
get_function_hook('builtins.list')
get_function_hook('__iter__ of list')
get_attribute_hook('typing.Iterator.__next__')
get_function_hook('__next__ of Iterator')
get_function_hook('builtins.print')
get_method_hook('builtins.list.__getitem__')
get_function_hook('builtins.print')
get_function_hook('__setitem__ of list')
get_method_signature_hook('builtins.list.clear')
get_method_hook('builtins.list.clear')
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongpriority-0-hightopic-pluginsThe plugin API and ideas for new pluginsThe plugin API and ideas for new plugins