gh-142750: Normalize ParamSpec __bound__ when bound is None#142751
gh-142750: Normalize ParamSpec __bound__ when bound is None#142751hyongtao-code wants to merge 4 commits into
Conversation
When creating a ParamSpec with bound=None, the runtime __bound__ attribute is set to <class 'NoneType'> instead of None. This change mirrors TypeVar.__new__ by explicitly converting Py_None to NULL before type checking, restoring the correct semantics and ensuring consistency between TypeVar and ParamSpec. Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com>
JelleZijlstra
left a comment
There was a problem hiding this comment.
Thanks, I agree we should change this. Can you add a test?
There was a problem hiding this comment.
Good catch. None is the default value listed in the docs: https://docs.python.org/3/library/typing.html#typing.ParamSpec So, it should not be set to NoneType 100%
Please, add a test case and add a news entry :)
|
Thanks for the review! 🙂 Test case passed locally. D:\MyCode\cpython>PCbuild\amd64\python_d.exe -m test test_typing.py
Using random seed: 3794058828
0:00:00 Run 1 test sequentially in a single process
0:00:00 [1/1] test_typing
0:00:02 [1/1] test_typing passed
== Tests result: SUCCESS ==
1 test OK.
Total duration: 2.0 sec
Total tests: run=715 skipped=1
Total test files: run=1/1
Result: SUCCESS |
|
Thinking about this more, did this case ever come up with TypeVar? This seems sort of wrong: Having no bound and having a bound of None mean very different things to a type checker, yet they have the same runtime representation. I feel like this came up before and maybe we just decided not to care, since a bound of None is not very useful at type checking time? |
|
Yes, we decided to not care, because |
|
This PR is stale because it has been open for 30 days with no activity. |
When creating a ParamSpec with bound=None, the runtime bound attribute is set to <class 'NoneType'> instead of None.
This change mirrors TypeVar.new by explicitly converting Py_None to NULL before type checking, ensuring consistency between TypeVar and ParamSpec.
Steps to reproduce
Result without the patch
Result with the patch