-
-
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
Require an annotation if inferred list or dict item type is object #3816
Comments
#4548 has relevant discussion. |
Marking as high priority since this seems to be a common source of confusion. |
Working on this. |
Should this require a flag to enable it? (Or perhaps allow a flag to disable it?) |
I'm in support of a flag to disable. It is a backwards incompatible change, but no heavy refactoring is required to fix existing code, only taking the inferred types that the mypy errors generate and applying them to the codebase. Disabling by default would not fix the issue of new users being confused by the inference, since they would also presumably not know about the flag. |
#6805 has a similar example where we unexpectedly infer |
It would be great to sometimes suggest using |
Is this ever gonna be a thing? |
We might want to also do this for sets (#8947). Also, there will need to be a way to disable this, but not enabling this by default would be a bit pointless, as discussed above. |
Hi! I had been looking at this issue and the PR #6792 that's already opened for it. Since there is no activity on the PR for over a year, can I start a new one from where it left? I believe what's remaining is to fix broken test cases and add a few new ones? I'll try to make it work for sets and the optional flag as discussed above. |
@ChetanKhanna Sure, feel free to continue working on it! |
This PR does the following: - Fixes failing tests from PR 6792 - Adds suugested annotation note - Adds annotation checker for sets - Adds tests
I opened a PR for this: #9051 |
This PR does the following: - Fixes failing tests from PR 6792 - Adds suugested annotation note - Adds annotation checker for sets - Adds tests
Mypy infers the type
List[object]
forx
in this example:This is often not what the user expects and can be confusing, as there tends to be an error when the program tries to do something with the list. Often the user would prefer the type to be
List[Any]
orList[Union[int, str]]
, but generally the desired item type could be something else, such as an ABC or protocol (once protocols are supported).It's perhaps impractical to unambiguously infer the desired type, but it would be easy enough to detect cases like these and require an explicit annotation. For the above example, mypy would require an annotation for
x
(and potentially suggestList[Any]
orList[Union[int, str]]
as the potential type), similar to how mypy deals withx = []
.For user-defined generic types
object
might be a reasonable value for a type parameter, so we should probably restrict this to things where theobject
type is likely a problem. For example, we could generate the message for the typesList[object]
andDict[<anything>, object]
only. It's less clear if we should do this for dictionaries withobject
keys or for sets.The text was updated successfully, but these errors were encountered: