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 ClassVar
class Foo:
x: ClassVar[int | str]
class Bar(Foo):
x = "s"
class Baz(Bar):
x = 0 # womp womp
reveal_type(Baz.x)
Expected Behavior
The type didn't get narrowed when inheriting from Bar such that I can still assign x to int.
Note pyright doesn't have this particular affliction.
Actual Behavior
# Incompatible types in assignment (expression has type "int", base class "Bar" defined the type as "str") [assignment]
Your Environment
(See playground link))
The text was updated successfully, but these errors were encountered:
Note that pyright isn't narrowing types (since it infers int | str, not int), it's using the context of the parent type to determine the type in the Bar class. This is a missing feature from mypy, but I'd track this in e.g. #10375
This allows for lazilly-deferred `f`-strings to be used in `Target` and
`Field` help messages, allowing for forward-referencing classes in the
same module.
Due to some [annoyance in
`mypy`](python/mypy#14702), this also adds a
`help_text` helper function that ensures that types aren't narrowed by
subclasses of `Field` or `Target`. This, unfortunately, represents the
bulk of this PR.
This allows for lazilly-deferred `f`-strings to be used in `Target` and
`Field` help messages, allowing for forward-referencing classes in the
same module.
Due to some [annoyance in
`mypy`](python/mypy#14702), this also adds a
`help_text` helper function that ensures that types aren't narrowed by
subclasses of `Field` or `Target`. This, unfortunately, represents the
bulk of this PR.
Bug Report
I think the reproduction code speaks for itself 😅
To Reproduce
Playground URL: https://mypy-play.net/?mypy=latest&python=3.11&gist=333f013c023a7d031cf8ecfb30143bc3
Code:
Expected Behavior
The type didn't get narrowed when inheriting from
Bar
such that I can still assignx
toint
.Note
pyright
doesn't have this particular affliction.Actual Behavior
# Incompatible types in assignment (expression has type "int", base class "Bar" defined the type as "str") [assignment]
Your Environment
(See playground link))
The text was updated successfully, but these errors were encountered: