Skip to content
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

fix: error checking inheritance when using PEP585 and PEP604 type hints #3681

Merged
merged 15 commits into from May 14, 2022

Conversation

aleksul
Copy link
Contributor

@aleksul aleksul commented Jan 17, 2022

Change Summary

Created convert_generics function, which searches for str types inside of type hints and replaces them with ForwardRef.
Used this function in ModelField.

Related issue number

fix #2798
fix #3297

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/<pull request or issue id>-<github username>.md file added describing change
    (see changes/README.md for details)
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Watch cython issue cython/cython#2753
Previous implementation can be used after cython 3.0 release
@aleksul
Copy link
Contributor Author

aleksul commented Jan 19, 2022

please review

@aleksul aleksul marked this pull request as ready for review January 19, 2022 16:11
@aleksul aleksul changed the title Fix "error checking inheritance of" when using PEP585 and PEP604 type hints fix: error checking inheritance when using PEP585 and PEP604 type hints Mar 19, 2022
@gghez
Copy link

gghez commented Mar 19, 2022

hi, when this fix is planned to be released please?

@aleksul
Copy link
Contributor Author

aleksul commented Mar 20, 2022

hi, when this fix is planned to be released please?

No idea actually. It first must be reviewed by maintainers. Until then, you can install this bugfix using the command below.

pip install git+https://github.com/aleksul/pydantic.git@c421630a9da2d98602e7fca4b555c5f390311774

It should be ok, I've tested it on CI in my repo.

Scheduled for v1.9.1

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good, thank you for the contribution.

Some things to change.

pydantic/typing.py Outdated Show resolved Hide resolved
pydantic/typing.py Outdated Show resolved Hide resolved
pydantic/typing.py Outdated Show resolved Hide resolved
tests/test_forward_ref.py Outdated Show resolved Hide resolved

@pytest.mark.skipif(sys.version_info < (3, 9), reason='PEP585 generics only supported for python 3.9 and above.')
def test_convert_generics():
assert convert_generics(int) == int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we parameterise these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can but Python does actually run all of the stuff inside of pytest.mark.parametrize (even if the test should be skipped), so parametrized variables can only be strings, otherwise Python 3.7 and 3.8 tests will fail.
Anyway, I've done the parametrization. It looks a little bit weird (but cleaner), involved eval magic, and also made me wrote noqa: F401 to suppress flake8's "unused import" warning. Maybe you have an idea for a better solution?

@samuelcolvin
Copy link
Member

please update.

@tyteen4a03
Copy link

Any updates to this PR?

@samuelcolvin
Copy link
Member

I've removed the 1.9.1 milestone as, on reflection, I don't think this is a regression in v1.9.

@samuelcolvin samuelcolvin removed this from the v1.9.1 milestone May 11, 2022
@aleksul
Copy link
Contributor Author

aleksul commented May 12, 2022

I've removed the 1.9.1 milestone as, on reflection, I don't think this is a regression in v1.9.

I believe this is a regression indirectly because since Python 3.9 introduced PEP585, all of the type hints (such as List, Dict, Tuple, etc.) in the typing module were marked as deprecated, and it leads to a weird situation: on the one side - you don't want to write new code with deprecated stuff, on the other - you can't because of this annoying bug. Also, PEP604 was introduced in Python 3.10, and (as far as I understood from the release note) pydantic 1.9.0 should bring support for it, so if not a regression, this is at least an overlooked use case, I believe.
It's especially bad when using an ORM (because they heavily rely on relation checks inside of pydantic which is what this bug is all about). So, with or without the milestone, I hope this PR (or possibly another one fixing the same issue with a different approach) will be merged ASAP.

@samuelcolvin samuelcolvin added this to the v1.9.1 milestone May 12, 2022
@samuelcolvin
Copy link
Member

Makes sense, I've re-added the milestone and will try to include this.

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise LGTM.

This looks excellent, thank you so much for the really well thought out PR.

tests/test_forward_ref.py Outdated Show resolved Hide resolved
tests/test_typing.py Outdated Show resolved Hide resolved
@samuelcolvin
Copy link
Member

please update. Also, please could you resolve conflicts - should be trivial.

@aleksul aleksul force-pushed the fix-generics-forward-refs branch from e91ea65 to a41c191 Compare May 13, 2022 23:50
@aleksul
Copy link
Contributor Author

aleksul commented May 14, 2022

please review

@github-actions github-actions bot assigned PrettyWood and samuelcolvin and unassigned aleksul May 14, 2022
@samuelcolvin samuelcolvin mentioned this pull request May 14, 2022
11 tasks
@samuelcolvin samuelcolvin merged commit f69012a into pydantic:master May 14, 2022
@samuelcolvin
Copy link
Member

thanks so much.

@aleksul aleksul deleted the fix-generics-forward-refs branch May 18, 2022 20:11
ntaylorwss pushed a commit to nicejobinc/pydantic that referenced this pull request Jun 24, 2022
…ints (pydantic#3681)

* Add tests

* Fix the issue

* Add changes file

* Improved convert_generics

* Add default fallback to convert_generics
Improved Annotated and Literal handling

* Fix Cython doesn't support generic types (PEP560)
Watch cython issue cython/cython#2753
Previous implementation can be used after cython 3.0 release

* Add custom type test

* Cosmetic fixes

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* Fix typos

* Add SelfReferencing test validation
Add parametrization to

* Fix: parametrization caused test discovery problem

* Better explanation for a test case

* Better assertions for model creation tests

* Rerun CI

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants