8000 Support usage of `type` with `typing.Self` and type aliases by kc0506 · Pull Request #10621 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Support usage of type with typing.Self and type aliases #10621

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

Merged
merged 19 commits into from
Oct 17, 2024

Conversation

kc0506
Copy link
Contributor
@kc0506 kc0506 commented Oct 14, 2024

Change Summary

Fix errors caused by these:

TypeAdapter(type[Self])

type MyInt = int
TypeAdapter(type[MyInt])

Related issue number

fix #10618

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

@github-actions github-actions bot added the relnotes-fix Used for bugfixes. label Oct 14, 2024
@kc0506 kc0506 changed the title Fix error caused by type[Self] Fix error caused by type[Self]; add typecheck for type[] Oct 14, 2024
@kc0506 kc0506 changed the title Fix error caused by type[Self]; add typecheck for type[] Fix error caused by type[Self]; check the content of type[] is a class Oct 14, 2024
Copy link
codspeed-hq bot commented Oct 14, 2024

CodSpeed Performance Report

Merging #10621 will not alter performance

Comparing kc0506:fix-type-of-self (12a9c26) with main (7bb0e0a)

Summary

✅ 44 untouched benchmarks

Copy link
Contributor
github-actions bot commented Oct 14, 2024

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic/_internal
  _generate_schema.py 1698
Project Total  

This report was generated by python-coverage-comment-action

@kc0506 kc0506 changed the title Fix error caused by type[Self]; check the content of type[] is a class Fix error caused by type[Self] and type[TypeAliasType(...)] Oct 14, 2024
@kc0506 kc0506 changed the title Fix error caused by type[Self] and type[TypeAliasType(...)] Fix errors generating schemas of type[Self] and type[TypeAliasType(...)] Oct 14, 2024
Copy link
Member
@Viicos Viicos left a comment

Choose a reason for hiding this comment

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

Thanks for looking at this!

@Viicos Viicos changed the title Fix errors generating schemas of type[Self] and type[TypeAliasType(...)] Support usage of type with typing.Self and type aliases Oct 14, 2024
Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
Copy link
Contributor
@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

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

Looking good, nice work on the tests.

Wanted to resolve some questions about the _typing_extra.eval_type call - perhaps we could jump right to a type alias schema if one is detected...

@kc0506
Copy link
Contributor Author
kc0506 commented Oct 16, 2024

I tried this snippet in Python 3.8, 3.10 and 3.12. Something interesting happed.

tp=List['int']
tp_eval=typing._eval_type(tp, globals(), locals())
print(typing.get_args(tp)) 
print(typing.get_args(tp_eval))  

tp=list['int']
tp_eval=typing._eval_type(tp, globals(), locals())
print(typing.get_args(tp))
print(typing.get_args(tp_eval))

The results are:

Python version Args of List['int'] Args of List['int'], evaled Args of list['int'] Args of list['int'], evaled
3.8 ForwardRef('int') int N.A. N.A.
3.9-3.11 ForwardRef('int') int 'int' 'int'
3.12 ForwardRef('int') int ForwardRef('int') int

So this means (only?) when user annotate with list['int'] in Python 3.9-3.11, we will encounter str instead of ForwardRef.

@Viicos
Copy link
Member
Viicos commented Oct 17, 2024

So this means (only?) when user annotate with list['int'] in Python 3.9-3.11, we will encounter str instead of ForwardRef.

This was done in python/cpython#30900.

Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
Copy link
Member
@Viicos Viicos left a comment

Choose a reason for hiding this comment

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

Thanks!

@Viicos Viicos merged commit f0002fc into pydantic:main Oct 17, 2024
61 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes-fix Used for bugfixes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

type[Self] raises TypeError in pydantic-core
3 participants
0