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
from __future__ import annotations
from typing import Optional
class C1(object):
def foo(self, a: C, b: C) -> None:
reveal_type(a) # Optional[C1]
reveal_type(b) # Any
def bar(self, a: C) -> C:
reveal_type(a) # Optional[C1]
return a
def baz(self) -> C:
return C()
C = Optional[C1]
def main() -> None:
reveal_type(C1().foo) # def (a: Optional[C1], b: Any)
reveal_type(C1().bar) # def (a: Optional[C1]) -> Any
reveal_type(C1().baz) # def () -> Optional[C1]
Note that C is defined after the class body of C1, but C is used in several type annotations in the body of C1 (taking advantage of from __future__ import annotations).
In each method of C1, the first occurrence of C is correctly used by mypy, but subsequent occurrences (in later parameter types or the return type) are incorrectly interpreted as Any.
If I move the alias definition C = Optional[C1] above the class body (and fix the resulting forward reference using a string), then everything works.
If this is the expected behavior, then consider this issue a request for a better error message than silently inferring Any.
I observe this behavior both on mypy version 0.650 and on the most recent master (01c268644).
The text was updated successfully, but these errors were encountered:
tstdefer.py:6: note: Revealed type is "Union[tstdefer.C1, None]"
tstdefer.py:7: note: Revealed type is "Union[tstdefer.C1, None]"
tstdefer.py:10: note: Revealed type is "Union[tstdefer.C1, None]"
tstdefer.py:14: error: "<typing special form>" not callable [operator]
tstdefer.py:19: note: Revealed type is "def (a: Union[tstdefer.C1, None], b: Union[tstdefer.C1, None])"
tstdefer.py:20: note: Revealed type is "def (a: Union[tstdefer.C1, None]) -> Union[tstdefer.C1, None]"
tstdefer.py:21: note: Revealed type is "def () -> Union[tstdefer.C1, None]"
Uh oh!
There was an error while loading. Please reload this page.
Consider the following code.
Note that
C
is defined after the class body ofC1
, butC
is used in several type annotations in the body ofC1
(taking advantage offrom __future__ import annotations
).In each method of
C1
, the first occurrence ofC
is correctly used by mypy, but subsequent occurrences (in later parameter types or the return type) are incorrectly interpreted asAny
.If I move the alias definition
C = Optional[C1]
above the class body (and fix the resulting forward reference using a string), then everything works.If this is the expected behavior, then consider this issue a request for a better error message than silently inferring
Any
.I observe this behavior both on mypy version 0.650 and on the most recent master (
01c268644
).The text was updated successfully, but these errors were encountered: