-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
TypeError: _eval_type() missing 1 required positional argument: 'type_params' with import module #118418
Comments
Well, this has several points.
This is hard :) |
See my sobolevn@d7483de commit that adds |
🤔
|
You closed the issue too early :) |
@enefry, I can't reproduce this issue based on the snippet you gave above: (3123-env) % python -m pip freeze
annotated-types==0.6.0
anyio==4.3.0
certifi==2024.2.2
distro==1.9.0
fastapi==0.110.3
h11==0.14.0
httpcore==1.0.5
httpx==0.27.0
idna==3.7
openai==1.24.0
pydantic==2.7.1
pydantic_core==2.18.2
sniffio==1.3.1
starlette==0.37.2
tqdm==4.66.2
typing_extensions==4.11.0
(3123-env) % python --version
Python 3.12.3
(3123-env) % python
Python 3.12.3 (main, Apr 30 2024, 10:12:02) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import openai
>>> import fastapi
>>> exit() Could you post a minimal, reproducible example please? :-) |
you may checkout commit before this commit my library version is :
|
Thanks @enefry, I can reproduce with a fresh build of the tip of the 3.12 branch. Apologies -- I assumed you were experiencing the bug with a released version of Python 3.12. |
To summarise, then: A change to The change in question fixed a bug where I want to say that "this is what happens if you make use of undocumented implementation details" and close the issue as "not a bug". But obviously pydantic is a very popular framework, and it's not great that it would be broken by a patch release of CPython. Breaking pydantic means that other very popular libraries such as @sobolevn has floated adding a default value for the new parameter for Cc. @samuelcolvin for awareness. |
Yes, this is what I said in #118418 (comment)
Maybe there's a better way :) |
This bug is especially bad because it triggers at import time, meaning that affected libraries cannot be used at all in combination with affected versions of CPython. I vote for adding the default on 3.12. We should avoid breaking half the ecosystem in a bugfix release. For 3.13 maybe the best solution would be to expose a new public function for whatever Pydantic needs |
Let's say that we add a default value for the parameter. I'd assume that pydantic will also get incorrect behaviour that could result in a |
I guess this would be the compat code to workaround this problem: if sys.version_info < (3, 12, 5): # or whatever version it would be
# Python has a bug in its `_eval_type` function.
locals_namespace = {} # whatever they are
if sys.version_info >= (3, 12):
# Python since 3.12.0 and until 3.12.4 also has a bug with NameError
# for types using PEP695 type params:
locals_namespace.update({
param.__name__: param
for param in cls_or_func.__type_params__
})
result = typing._eval_type(type_, globals_namespace, locals_namespace, recursive_guard=...)
else:
result = typing._eval_type(
type_, globals_namespace, locals_namespace,
cls_or_func.__type_params__, # since 3.12.5 `_eval_type` has a correct way to do it
recursive_guard=...,
) PR is on its way. |
Okay, if it was "just" pydantic that was going to be broken with this, then I would have argued that they'd have plenty of time to patch their code before the release of Python 3.12.4, and that adding a default value for this parameter would just mean that pydantic would have the same bug in their use of
We can't afford to break this many libraries in a patch release, so I now agree that we have no choice except to add a default value to the new parameter. After #118431 is merged, however, I would like to propose that we immediately make a Python 3.13-only change to start issuing a |
…al_type` (pythonGH-118431) (cherry picked from commit 4a5ad84) Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Alternate suggestion: in 3.12.4 and successors, default to a private sentinel so as to detect defaulting versus explicitly passing None. If not overriden, convert it to None and issue a deprecation warning. Keep the proper signature in 3.13. There will be months for users of the private function to adjust before the latter's release. After .0b1, we can discuss making the function public, or making some other change. |
Unfortunately, lots of users run tests with Possibly we could consider being aggressive with the deprecation period, but I think if we've conceded that this change could break quite a few libraries (even if it shouldn't), we may as well go through the standard two-cycle deprecation period for removing the default. |
I propose not to touch |
…s* parameter of some private `typing` APIs
I don't think that's sufficient: there are already many libraries using the private API, and even if we provide a public API for them in 3.14, it will take time for them to migrate to the new public API. In the meantime, the footgun will continue to exist in the private API that they're already using. I've put up #118695 to deprecate failing to pass a value to the new parameter. It's a pretty simple change and I think we should go ahead with it. |
…ameter of some private `typing` APIs (#118695)
…s* parameter of some private `typing` APIs (python#118695)
Hi everyone, sorry I somehow missed the notification for this one. @AlexWaygood thanks so much for accommodating my egregious use of a private API. We'll get Pydantic working with 3.12.4 and 3.13 ASAP. |
Bug report
What happened?
CPython versions tested on:
3.12
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
Python 3.12.3+ (heads/3.12-dirty:817190c, Apr 29 2024, 12:17:20) [GCC 11.4.0]
Linked PRs
type_params
intyping._eval_type
#118431type_params
intyping._eval_type
(GH-118431) #118436typing
APIs #118695The text was updated successfully, but these errors were encountered: