-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
No type inference for dicts in union #6463
Comments
I’m sure the cause is the union, not the tuple. |
@gvanrossum thanks, you're right - corrected the issue title and the sample code |
@ikonst you could try this to have a workaround, though the semantics has changed
|
This may be a duplicate of #2164 |
@oraluben - the actual code is more complex: from typing import *
from flask import Response
JsonDict = Dict[str, Any]
_FlaskResponse = Union[
str,
bytes,
Callable,
Response,
]
_FlaskRestfulResponse = Union[
JsonDict,
_FlaskResponse,
]
FlaskRestfulMethodReturnType = Union[
_FlaskRestfulResponse,
Tuple[Optional[_FlaskRestfulResponse], int], # (response, status_code)
Tuple[Optional[_FlaskRestfulResponse], int, Dict[str, str]], # (response, status_code, headers)
Tuple[Optional[_FlaskRestfulResponse], Dict[str, str]], # (response, headers)
] and then this works: class MyResource(Resource):
def get() -> FlaskRestfulMethodReturnType:
return {} and this works: class MyResource(Resource):
def get() -> FlaskRestfulMethodReturnType:
return {"foo": "bar"}, 200 but this doesn't: class MyResource(Resource):
def get() -> FlaskRestfulMethodReturnType:
return {}, 200 |
This happens with other invariant containers as well, such as lists. I just encountered an instance where mypy couldn't infer the type of a |
This also happens with dicts inside a tuple: from typing import Dict, Tuple, Union
T = Dict[str, str]
U = Tuple[T]
def f() -> U:
return {},
def g() -> Union[U, U]:
return {},
|
You can work around this issue with:
i.e., create a function to create an empty container with a defined type.
|
This false-positive is also there with unions of lists when assigning an empty list (e.g. The issue is a few years old at this point but the case is not obscure enough that it would only occur in very few codebases. What's the status on this? |
Thanks @JosuaKrause for the work around. This actually can be done very concisely in newer python versions by using foo: list[str] | list[int] = list[str]() # fine
bar: list[str] | list[int] = list[int]() # fine
baz: list[str] | list[int] = [] # error |
Fixes #230 Fixes #6463 I bet it fixes some other duplicates, I closed couple yesterday, but likely there are more. This may look a bit ad-hoc, but after some thinking this now starts to make sense to me for two reasons: * Unless I am missing something, this should be completely safe. Special-casing only applies to inferred types (i.e. empty collection literals etc). * Empty collections _are_ actually special. Even if we solve some classes of issues with more principled solutions (e.g. I want to re-work type inference against unions in near future), there will always be some corner cases involving empty collections. Similar issues keep coming, so I think it is a good idea to add this special-casing (especially taking into account how simple it is, and that it closer some "popular" issues).
In
foo
,{}
is inferred asDict[str, Any]
.In
bar
, I get:The text was updated successfully, but these errors were encountered: