KEMBAR78
Plugin hooks are not called or receive incorrect names for some dunder methods · Issue #4964 · python/mypy · GitHub
Skip to content

Plugin hooks are not called or receive incorrect names for some dunder methods #4964

@dgelessus

Description

@dgelessus

(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_hook is incorrectly called instead of get_method_hook, and the passed in method name is not fully qualified
  • get_method_signature_hook is 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 to get_function_hook is 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

No one assigned

    Labels

    bugmypy got something wrongpriority-0-hightopic-pluginsThe plugin API and ideas for new plugins

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions