You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Decorator on __init__ causes loss of type info #5398
Adding annotations to __init__ does not change the result; here's the updated code:
from __future__ import annotations
from typing import Callable
def decorator(method: Callable[[Foo], None]) -> Callable[[Foo], None]:
return method
class Foo():
@decorator
def __init__(self: Foo) -> None:
pass
reveal_type(Foo.__init__)
I get the same result (the revealed type is Any). If I change the name of the method, the type inference works correctly, so this issue seems to be specific to __init__.
I just stumbled at this again. Even precisely typed decorator on __init__ not just erases types, it completely erases the signature of __init__. For example:
It looks like #1706 is a special case of this. FYI, I am working on a PR to fix this. It seems like the problem is that quite a few places check whether a node represents a callable function by checking if it is an instance of FuncBase, which Decorator is not. We don't want to make Decorator inherit from FuncBase, because whether it is callable or not depends on the type returned by the decorator, and we don't want to assume that it is, so my thought was to override __instancecheck__ in FuncBase to specifically check and return true if the decorated function is callable. There I still some issues I need to work out with my implementation—my current version has some false positives that I am trying to track down, but if anyone would like to offer input on what I'm doing, my code is at https://github.com/TV4Fun/mypy/tree/fix_decorators
Activity
gvanrossum commentedon Jul 27, 2018
Try again after adding
-> Noneto the__init__signature.campkeith commentedon Jul 29, 2018
Adding annotations to
__init__does not change the result; here's the updated code:I get the same result (the revealed type is
Any). If I change the name of the method, the type inference works correctly, so this issue seems to be specific to__init__.[-]Method decorator causes loss of type info[/-][+]Decorator on __init__ causes loss of type info[/+]gvanrossum commentedon Jul 29, 2018
__init__is special in a number of ways, but this still seems wrong.ilevkivskyi commentedon Sep 17, 2018
I just stumbled at this again. Even precisely typed decorator on
__init__not just erases types, it completely erases the signature of__init__. For example:JukkaL commentedon Sep 18, 2018
Increased priority to "high" since this behavior is very unexpected and can hide bugs.
TV4Fun commentedon Oct 13, 2018
It looks like #1706 is a special case of this. FYI, I am working on a PR to fix this. It seems like the problem is that quite a few places check whether a node represents a callable function by checking if it is an instance of
FuncBase, whichDecoratoris not. We don't want to makeDecoratorinherit fromFuncBase, because whether it is callable or not depends on the type returned by the decorator, and we don't want to assume that it is, so my thought was to override__instancecheck__inFuncBaseto specifically check and return true if the decorated function is callable. There I still some issues I need to work out with my implementation—my current version has some false positives that I am trying to track down, but if anyone would like to offer input on what I'm doing, my code is at https://github.com/TV4Fun/mypy/tree/fix_decoratorsIntroduce mypy stub for typeguard, and type-check parsl.load()
Introduce mypy stub for typeguard, and type-check parsl.load() (#874)
Support decorated constructors (#6645)