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
Add eval_type_backport to handle union operator in older Pythons #7958
Conversation
… properly, clean up type ignore comments
please review |
Thanks for the contributions. Let's wait until tomorrow until @dmontagu can give some feedback to merge this. I think this would be fine to include in a patch release, effectively as a bug fix, after we release |
@PrettyWood tried this long ago, I personally think it's too complicated and confusing to be worth while. |
@samuelcolvin We couldn't do it before because of cython. Without it it's doable. |
Closing this 👍 |
Well that's disappointing. I questioned whether it was worth it in the original issue and I explained that I was going to use AST modification. @PrettyWood your last comment there seemed OK with this approach, did I misunderstand or was this not what you were expecting?
@samuelcolvin was this referring to @PrettyWood's approach that manipulated tokens, or the AST approach here? I'm honestly surprised to hear that it's considered complicated/confusing/hacky, it's a small amount of pretty straightforward code that's working well and has no issues that I know of. What if we make it optional and off by default, so you have to e.g. turn on an environment variable to enable it? Everything would still go through |
I didn't say I'm against it. I see the added value. I just don't know if it's pydantic job to add that kind of logic. |
Hi @alexmojaki sorry to disappoint. I'm just a bit scared that:
Still, I have wanted this a number of times, so here's my proposal: @alexmojaki would you consider packaging this as a new library, pydantic can add it as an optional depedency and try to import it and fall back to the current behaviour when it's not installed, or not required. We can add some explanation and a link to the package's docs to our docs. That way it's isolated, off by default and the code doesn't need to live in pydantic or have the same quality/maintenance guarentees that pydantic does. WDYT? |
Thanks, that's a fair and helpful explanation. I was also thinking that a separate library was a likely solution, I'll do that. |
PR with separate library: #8209 |
Change Summary
Replaces all calls (outside v1) to
typing._eval_type
with the newtyping_extra.eval_type_backport
which uses AST modification to convertX | Y
toUnion[X, Y]
when needed. This is only used when an appropriate TypeError is raised so it should usually only kick in on older Python versions. It might also help with cases like #7875 where|
is simply unimplemented but probably shouldn't be treated as a complete solution for that kind of thing.Related issue number
Closes #7873
Checklist
Selected Reviewer: @hramezani