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 BaseModel type annotations to be resolvable by typing.get_type_hints #7680

Merged
merged 6 commits into from
Feb 13, 2024

Conversation

devmonkey22
Copy link
Contributor

@devmonkey22 devmonkey22 commented Sep 27, 2023

Fix BaseModel type annotations to be resolvable by typing.get_type_hints (#7623)

Change Summary

Updated required main.py type definitions used with non-deprecated BaseModel methods to always be defined, instead of conditionally when typing.TYPE_CHECKING.

Related issue number

Fix #7623

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @davidhewitt

@sydney-runkle
Copy link
Member

@devmonkey22,

Thanks for your contribution here. Is this ready to review? If so, could you please rebase to resolve the conflicts with main?

@sydney-runkle sydney-runkle added the relnotes-fix Used for bugfixes. label Jan 25, 2024
Copy link

codspeed-hq bot commented Jan 25, 2024

CodSpeed Performance Report

Merging #7680 will not alter performance

Comparing devmonkey22:fix-model-type-hints (2cbece8) with main (a280f13)

Summary

✅ 10 untouched benchmarks

@sydney-runkle
Copy link
Member

Currently running into some test issues related to this: #8307

@sydney-runkle
Copy link
Member

I think the right path forward here is to implement a solution that works for Python 3.10+ and add xfails to tests for 3.8 and 3.9.

@sydney-runkle sydney-runkle marked this pull request as ready for review February 13, 2024 17:29
@sydney-runkle
Copy link
Member

Please review

Copy link
Contributor

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

LGTM, with just a brief question on the tests which may or may not be worth following up on.


def inspect_type_hints(
obj_type, members: Optional[Set[str]] = None, exclude_members: Optional[Set[str]] = None, recursion_limit: int = 3
):
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it make sense to return the hints from this function, so that they can be asserted to be of the correct form? Or is there just way too much stuff there?

Copy link
Member

Choose a reason for hiding this comment

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

Good question. They're pretty gnarly, so I'd opt to skip returning for now. Down the line, if someone's feeling quite invested, they could make the addition, but I don't see it as a blocker on this PR.

@sydney-runkle sydney-runkle merged commit 3d1355f into pydantic:main Feb 13, 2024
54 checks passed
Copy link
Contributor

@alexmojaki alexmojaki left a comment

Choose a reason for hiding this comment

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

welp, i was in the process of reviewing


TupleGenerator = typing.Generator[typing.Tuple[str, Any], None, None]
Model = typing.TypeVar('Model', bound='BaseModel')
# should be `set[int] | set[str] | dict[int, IncEx] | dict[str, IncEx] | None`, but mypy can't cope
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment should be moved. Or better yet addressed - can mypy not handle pydantic.config.JsonValue?

pydantic/main.py Show resolved Hide resolved
tests/test_type_hints.py Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BaseModel method type annotations cause typing.get_type_hints to raise NameError
4 participants