8000 Raise a better error when a generic alias is used inside `type[]` by Viicos · Pull Request #11088 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Raise a better error when a generic alias is used inside type[] #11088

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 1 commit into from
Dec 13, 2024

Conversation

Viicos
Copy link
Member
@Viicos Viicos commented Dec 11, 2024

As per #11000 (comment).

Change Summary

Related issue number

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 Dec 11, 2024
if _typing_extra.is_generic_alias(type_param):
raise PydanticUserError(
'Subscripting `type[]` with an already parametrized type is not supported. '
f'Instead of using type[{type_param!r}], use type[{_repr.display_as_type(get_origin(type_param))}].',
Copy link
Member Author

Choose a reason for hiding this comment

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

Because type_param is a generic alias, we can rely on its __repr__ implementation, which uses typing._type_repr to do a pretty representation already.

However, we still use our _repr.display_as_type (inspired by typing._type_repr) for the origin, which is not a generic alias anymore but a proper class.

This should cover most use cases.

Copy link
Contributor

Choose a reason for hiding this comment

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

This looks good. @dmontagu mentioned that we might want to support this eventually, so can we say "not yet supported", and link to the issue?

Copy link
Member Author

Choose a reason for hiding this comment

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

Imo trying to support this is going to introduce lots of maintenance burden for the reason I mentioned in #11000 (comment), and maybe not possible at all as mentioned in #10462 (comment).

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair enough, should we close #11000 then?

Copy link
codspeed-hq bot commented Dec 11, 2024

CodSpeed Performance Report

Merging #11088 will not alter performance

Comparing type-gen-alias-error (de4bbea) with main (cc01258)

Summary

✅ 46 untouched benchmarks

Copy link
Contributor

Coverage report

Click to see where and how coverage changed

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

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

@Viicos Viicos added relnotes-feature and removed relnotes-fix Used for bugfixes. labels Dec 11, 2024
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.

Thanks!

@Viicos Viicos merged commit c393317 into main Dec 13, 2024
55 checks passed
@Viicos Viicos deleted the type-gen-alias-error branch December 13, 2024 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0