8000 Add `len` to `_BaseUrl` to avoid TypeError by Kharianne · Pull Request #11111 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Add len to _BaseUrl to avoid TypeError #11111

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 2 commits into from
Dec 18, 2024

Conversation

Kharianne
Copy link
Contributor
@Kharianne Kharianne commented Dec 13, 2024

Change Summary

Add magic method to _BaseUrl class to support allowed usage of Field with max length with AnyUrl and simple length comparison and checks of AnyUrl types.

Related issue number

fix #11092

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: @sydney-runkle

@github-actions github-actions bot added the relnotes-fix Used for bugfixes. label Dec 13, 2024
Copy link
codspeed-hq bot commented Dec 13, 2024

CodSpeed Performance Report

Merging #11111 will not alter performance

Comparing Kharianne:add-len-baseurl (6149769) with main (acc5902)

Summary

✅ 46 untouched benchmarks

Copy link
Contributor
github-actions bot commented Dec 13, 2024

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  networks.py
Project Total  

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

@Kharianne
Copy link
Contributor Author

please review

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!

I think we can just to len(self._url) in the __len__ check.

Could you also please add this fix to _BaseMultiHostUrl and add a test for that as well?

We'll get this into our patch release tomorrow :)

@pydantic-hooky pydantic-hooky bot added awaiting author revision awaiting changes from the PR author and removed ready for review labels Dec 16, 2024
pydantic-hooky bot assigned Kharianne and unassigned sydney-runkle Dec 16, 2024
@Kharianne
Copy link
Contributor Author
Kharianne commented Dec 16, 2024

@sydney-runkle

No problem! :-)
However your proposal does not work:
len(self._url) --> 'pydantic_core._pydantic_core.Url' has no len()
That's why I needed to add the extra step to call the str method defined on _BaseUrl.

The self._url is a type Url from pydantic-core and this class is not of type Sized - not having len. Maybe this should be fixed in pydantic-core?

Edit: I added the fix to _BaseMultiHostUrl as well + test. But let me know if the fix should be here or in core.

@sydney-runkle
Copy link
Contributor

Hmm yeah, this makes sense I guess, we used to be able to apply directly to the core schema, and now we have this complex wrapped type.

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 for the tests! Looks good for now.

We can move the __len__ support to pydantic-core eventually...

@sydney-runkle sydney-runkle added backport-2.10 and removed awaiting author revision awaiting changes from the PR author labels Dec 18, 2024
@sydney-runkle sydney-runkle merged commit de50a9e into pydantic:main Dec 18, 2024
103 checks passed
sydney-runkle pushed a commit that referenced this pull request Dec 18, 2024
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.

AnyUrl: TypeError: object of type 'AnyUrl' has no len()
2 participants
0