-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Recursive type not working #15710
Comments
Update: I'm wrong. OP has an immutabledict that derives from The problem is that they're not compatible: d1 = MyDict()
d2: OUTPUT_BASE_TYPES = d1
assert isinstance(d2, immutabledict.immutabledict)
reveal_type(d2) # N: revealed type is "immutabledict.immutabledict[str, 'OUTPUT_BASE_TYPES']"
# this should be illegal since MyDict values are "str | None"
d2['foobar'] = 42
# and mypy still doesn't known:
reveal_type(d1['foobar']) # N: revealed type is "str | None"
reveal_type(d2['foobar']) # N: revealed type is "OUTPUT_BASE_TYPES" = "str | int | ..."
# and of course
assert d1['foobar'] == 42 |
Thank you for your answer. What are solutions here?
The simplest solution that I see is:
And that loses the recursive type definition. |
Actually I might be wrong here. For plain Assuming Gotta dig deeper here. |
You know, just by calling something _K = TypeVar("_K")
_V = TypeVar("_V")
class immutabledict(Mapping[_K, _V]):
... (juts as a reminder type variables are invariant by default) |
@ilevkivskyi thank you for the hint. Other immutable collections (Sequence/Tuple) are covariant per mypy docs. |
An issue should be filled against immutabledict |
Yup I did that here: corenting/immutabledict#243 |
Bug Report
I am using a recursive type to constrain int/str/float/None/bool/Tuple[recursive_type, ...],dict[str, recursive_type]
but mypy says that a return type that I am defining is incompatible even though it is a subset of the recursive type union.
To Reproduce
Why does mypy say that my return type is incompatible?
Here is my code:
But the definition of the values for immutabledict typing.Union[None, str] are a subset of the
OUTPUT_BASE_TYPES
union of typesso why is this failing?
How do i get it to work? What do I need to change?
Actual Behavior
mypy reports:
mypy union.py union.py:36: error: Incompatible types in assignment (expression has type "MyDict", base class "ApiResponse" defined the type as "Union[OUTPUT_BASE_TYPES, Unset]") [assignment]
Your Environment
pip install immutabledict
mypy union.py
mypy.ini
N/A[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
The text was updated successfully, but these errors were encountered: