-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Internal Error when overriding a generic variable in __init__ #5846
Comments
This still crashes on master, here is a bit more self-contained repro: from typing import Generic, TypeVar, Any
T = TypeVar('T')
class B(Generic[T]):
x: T
class C(B):
def __init__(self) -> None:
self.x: Any It actually crashes in several similar scenarios, the only requirement is that the variable type in a base class is generic, and it is overridden in a method, I wonder how no one found this before. This happens in the part of code that I never understood, there is a method called |
This crash appeared again in #6033 |
in addition |
I just hit this crash in version |
I hit this error in mypy 0.701, also with a method that overrides the type of a base class member that was generic in the base class. |
I found a workaround: from typing import Generic, TypeVar, TYPE_CHECKING
T = TypeVar('T')
class B(Generic[T]):
x: T
class C(B[int]):
if TYPE_CHECKING:
@property
def x(self) -> str: # type: ignore
return self.x
reveal_type(C().x) |
Fixes #5846 The fix for crash is kind of straightforward. Previously `active_self_type()` always returned `None` at method scope (sic!) While working on this I discovered couple other problems: * Area around checking LSP is generally problematic (see also #5425 that I tried to fix earlier but failed). In particular, the are two ways to get current `TypeInfo`, one is from `lvalue.node.info` or `defn.info`, another is using `active_self_type()` (this is rather abusing it IMO). I don't try to fix this here (i.e. switch to always using one way) because this is a relatively large refactoring. * Currently in type checker we defer nested functions instead of top-level ones. I believe this is not by design and is rather caused by absence of docstrings and unintuitive method names in `CheckerScope` class. Namely, there "top function" stays for "top of stack function", not "top-level function". I don't try to fix this here either because this is conceptually a big change.
This is a bug report.
I created a simple python file called breakmypy.py which has the following contents:
When I run
mypy breakmypy.py --show-traceback
the following output is emitted:breakmypy.py:14: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.630
The text was updated successfully, but these errors were encountered: