Require an annotation if inferred list or dict item type is object
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 28/100
Research direction
The reproducer is x = [1, 'a'], with the proposed behavior compared to mypy's handling of empty collections. Start by tracing inference for list and dictionary literals and the existing empty-collection annotation diagnostic. Done requires a decided scope for object-valued containers, an appropriate diagnostic, and coverage for the agreed cases.
Written by the indexing model from the issue text.
Description
Mypy infers the type List[object] for x in this example:
x = [1, 'a']
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] or List[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 suggest List[Any] or List[Union[int, str]] as the potential type), similar to how mypy deals with x = [].
For user-defined generic types object might be a reasonable value for a type parameter, so we should probably restrict this to things where the object type is likely a problem. For example, we could generate the message for the types List[object] and Dict[<anything>, object] only. It's less clear if we should do this for dictionaries withobject keys or for sets.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 54
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python/mypy
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
bug topic-configuration topic-error-reporting
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
🐛 Bug 🔔 Pending processing
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
jumpserver/jumpserver#17584 ·