-
-
Notifications
You must be signed in to change notification settings - Fork 1.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
Add more correct type annotations for tomllib
#10031
Conversation
Note to reviewers: mypy_primer won't be much use on this PR, since we run mypy_primer on Python 3.10, and tomllib is new in Python 3.11. |
This comment has been minimized.
This comment has been minimized.
We now run mypy_primer on 3.11 following #9336. I've merged |
Diff from mypy_primer, showing the effect of this PR on open source code: pytest (https://github.com/pytest-dev/pytest)
+ src/_pytest/config/findpaths.py:79: error: Item "int" of "Union[int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "get" [union-attr]
+ src/_pytest/config/findpaths.py:79: error: Item "float" of "Union[int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "get" [union-attr]
+ src/_pytest/config/findpaths.py:79: error: Item "str" of "Union[int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "get" [union-attr]
+ src/_pytest/config/findpaths.py:79: error: Item "List[_TOMLValue]" of "Union[int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "get" [union-attr]
+ src/_pytest/config/findpaths.py:79: error: Item "date" of "Union[int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "get" [union-attr]
+ src/_pytest/config/findpaths.py:79: error: Item "time" of "Union[int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "get" [union-attr]
+ src/_pytest/config/findpaths.py:79: error: Item "int" of "Union[Any, int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "get" [union-attr]
+ src/_pytest/config/findpaths.py:79: error: Item "float" of "Union[Any, int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "get" [union-attr]
+ src/_pytest/config/findpaths.py:79: error: Item "str" of "Union[Any, int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "get" [union-attr]
+ src/_pytest/config/findpaths.py:79: error: Item "List[_TOMLValue]" of "Union[Any, int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "get" [union-attr]
+ src/_pytest/config/findpaths.py:79: error: Item "date" of "Union[Any, int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "get" [union-attr]
+ src/_pytest/config/findpaths.py:79: error: Item "time" of "Union[Any, int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "get" [union-attr]
+ src/_pytest/config/findpaths.py:87: error: Key expression in dictionary comprehension has incompatible type "Union[Any, int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]"; expected type "str" [misc]
+ src/_pytest/config/findpaths.py:87: error: Item "int" of "Union[Any, int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "items" [union-attr]
+ src/_pytest/config/findpaths.py:87: error: Item "float" of "Union[Any, int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "items" [union-attr]
+ src/_pytest/config/findpaths.py:87: error: Item "str" of "Union[Any, int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "items" [union-attr]
+ src/_pytest/config/findpaths.py:87: error: Item "List[_TOMLValue]" of "Union[Any, int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "items" [union-attr]
+ src/_pytest/config/findpaths.py:87: error: Item "date" of "Union[Any, int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "items" [union-attr]
+ src/_pytest/config/findpaths.py:87: error: Item "time" of "Union[Any, int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "items" [union-attr]
pylint (https://github.com/pycqa/pylint)
+ pylint/config/find_default_config_files.py:29: error: Unsupported right operand type for in ("Union[int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]") [operator]
+ pylint/config/config_file_parser.py:71: error: Value of type "Union[int, float, str, bool, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], datetime, date, time]" is not indexable [index]
+ pylint/config/config_file_parser.py:71: error: No overload variant of "__getitem__" of "list" matches argument type "str" [call-overload]
+ pylint/config/config_file_parser.py:71: note: Possible overload variants:
+ pylint/config/config_file_parser.py:71: note: def __getitem__(self, SupportsIndex, /) -> _TOMLValue
+ pylint/config/config_file_parser.py:71: note: def __getitem__(self, slice, /) -> List[_TOMLValue]
+ pylint/config/config_file_parser.py:71: error: Invalid index type "str" for "Union[int, float, str, bool, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], datetime, date, time]"; expected type "Union[SupportsIndex, slice]" [index]
+ pylint/config/config_file_parser.py:77: error: Item "str" of "Union[Any, str, int, float, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "items" [union-attr]
+ pylint/config/config_file_parser.py:77: error: Item "int" of "Union[Any, str, int, float, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "items" [union-attr]
+ pylint/config/config_file_parser.py:77: error: Item "float" of "Union[Any, str, int, float, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "items" [union-attr]
+ pylint/config/config_file_parser.py:77: error: Item "List[_TOMLValue]" of "Union[Any, str, int, float, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "items" [union-attr]
+ pylint/config/config_file_parser.py:77: error: Item "date" of "Union[Any, str, int, float, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "items" [union-attr]
+ pylint/config/config_file_parser.py:77: error: Item "time" of "Union[Any, str, int, float, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" has no attribute "items" [union-attr]
+ pylint/config/config_file_parser.py:81: error: Invalid index type "Union[Any, int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" for "Dict[str, str]"; expected type "str" [index]
+ pylint/config/config_file_parser.py:85: error: Invalid index type "Union[Any, int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" for "Dict[str, str]"; expected type "str" [index]
poetry (https://github.com/python-poetry/poetry)
+ src/poetry/packages/locker.py:87: error: Unsupported right operand type for in ("Union[int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]") [operator]
+ src/poetry/packages/locker.py:88: error: Value of type "Union[int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]" is not indexable [index]
+ src/poetry/packages/locker.py:88: error: No overload variant of "__getitem__" of "list" matches argument type "str" [call-overload]
+ src/poetry/packages/locker.py:88: note: Possible overload variants:
+ src/poetry/packages/locker.py:88: note: def __getitem__(self, SupportsIndex, /) -> _TOMLValue
+ src/poetry/packages/locker.py:88: note: def __getitem__(self, slice, /) -> List[_TOMLValue]
+ src/poetry/packages/locker.py:88: error: Invalid index type "str" for "Union[int, float, str, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], date, time]"; expected type "Union[SupportsIndex, slice]" [index]
+ src/poetry/packages/locker.py:318: error: Item "int" of "Union[int, float, str, bool, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], datetime, date, time]" has no attribute "get" [union-attr]
+ src/poetry/packages/locker.py:318: error: Item "float" of "Union[int, float, str, bool, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], datetime, date, time]" has no attribute "get" [union-attr]
+ src/poetry/packages/locker.py:318: error: Item "str" of "Union[int, float, str, bool, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], datetime, date, time]" has no attribute "get" [union-attr]
+ src/poetry/packages/locker.py:318: error: Item "bool" of "Union[int, float, str, bool, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], datetime, date, time]" has no attribute "get" [union-attr]
+ src/poetry/packages/locker.py:318: error: Item "List[_TOMLValue]" of "Union[int, float, str, bool, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], datetime, date, time]" has no attribute "get" [union-attr]
+ src/poetry/packages/locker.py:318: error: Item "datetime" of "Union[int, float, str, bool, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], datetime, date, time]" has no attribute "get" [union-attr]
+ src/poetry/packages/locker.py:318: error: Item "date" of "Union[int, float, str, bool, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], datetime, date, time]" has no attribute "get" [union-attr]
+ src/poetry/packages/locker.py:318: error: Item "time" of "Union[int, float, str, bool, List[_TOMLValue], Dict[_TOMLValue, _TOMLValue], datetime, date, time]" has no attribute "get" [union-attr]
|
Just taking the first pytest error in the mypy_primer diff as an example, they're doing this: config = tomllib.loads(toml_text)
result = config.get("tool", {}).get("pytest", {}).get("ini_options", None) That seems like pretty idiomatic use of The stdlib Lastly, even if we decided that this PR is worth the disruption, it's not currently correct. The docs state:
That means that if the user provides a value to the |
Cc. @hauntsaninja & @hukkin for tomllib. |
I agree that the current annotation of |
Let's close this for now, then. Happy to reopen if @hauntsaninja or @hukkin disagree strongly with this decision :) |
I've added more correct type annotations for
tomllib
module, because it wasAny
, but there is a table of types in documentation.