-
-
Notifications
You must be signed in to change notification settings - Fork 1.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
python 3.9 generic alias Callable from collections module does not behave correctly in generics #2465
Comments
I can probably look at this this weekend... I don't think many people are using generics in 3.9 yet |
Yes you're right but supporting generic types seems to be rational, According to PEP585 legacy typing.List , typing.Dict , ... are Deprecated since python3.9 and will be removed from python. |
I don't know if it's really out of context but I recently gave a shot at using typingx to use get_args and get_origin with exact same behaviour for all python versions above 3.6 that mimic 3.10 (e.g. the origin is always collections.abc.Callable for a callable) |
@PrettyWood Ah yes, I think this will help with almost all of the problems I was thinking about that might potentially come up. I also commented on the general handling of using typing.XXXX vs. collections.abc.XXXX in tests here #2519 (comment) but I don't have a very clear opinion on that. |
@daviskirk I think this issue is actually more general than Here's an example: from typing import Generic, TypeVar, Mapping
from pydantic.generics import GenericModel, replace_types
KT = TypeVar('KT')
VT = TypeVar('VT')
class ModelWithMappingSubclass(GenericModel, Generic[KT, VT]):
class GenericMapping(Mapping[KT, VT]):
pass
mapping: GenericMapping[KT, VT]
assert replace_values(ModelWithMappingSubclass[KT, VT], {KT: str, VT: str}) == ModelWithMappingSubclass[str, str] I discovered this when trying to use |
Work on pydantic#2465 (see comments)
Work on pydantic#2465 (see comments)
Work on pydantic#2465 (see comments)
Work on pydantic#2465 (see comments)
Work on #2465 (see comments) Co-authored-by: Samuel Colvin <s@muelcolvin.com>
Work on #2465 (see comments) Co-authored-by: Samuel Colvin <s@muelcolvin.com>
Bug
Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:For example, the
test_iter_contained_typevars
test with replacingtyping.Callable
withcollections.abc.Callable
:results in
This came up while reading the release notes for python 3.10 where this is changed back: https://docs.python.org/3.10/whatsnew/3.10.html#collections-abc
So in 3.10 this problem won't be present.
However, since I don't think we're testing any of the "collection" module types particularly well, the assumption is that there might be other problems as well.
We should therefore probably add some tests that check these python >= 3.9 features / generic alias cases in case they are missing.
The text was updated successfully, but these errors were encountered: