Skip to content
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

Make detection of TypeVar defaults robust to the CPython PEP-696 implementation #536

Merged
merged 1 commit into from
May 12, 2024

Conversation

AlexWaygood
Copy link
Contributor

When implementing PEP 696 (default values for TypeVars, ParamSpecs and TypeVarTuples, we decided to implement the PEP slightly differently from the prototype that existed in typing_extensions. Specifically:

  • With the old implementation in typing_extensions:
    • If you passed default=None when constructing a TypeVar, the __default__ attribute would be set to types.NoneType
    • If you did not pass a value to the default= parameter, the __default__ attribute would be set to None
  • With the new implementation in CPython:
    • If you pass default=None when constructing a TypeVar, the __default__ attribute is set to None
    • If you do not pass a value to the default= parameter, the __default__ attribute is set to the new sentinel object typing.NoDefault.
  • The new implementation at CPython also has added a new has_default() method to TypeVars, ParamSpecs and TypeVarTuples

I backported these changes to the typing_extensions implementation of PEP 696 yesterday, and we realised today that it breaks some tests over at cattrs: python/typing_extensions#381. This PR fixes the tests so that they work if you're using a released version of typing_extensions, but they also work with the CPython implementation of PEP 696 and with the typing_extensions main branch.

I tested the PR locally by making this change to pyproject.toml:

--- a/pyproject.toml
+++ b/pyproject.toml
@@ -43,7 +43,7 @@ authors = [
 ]
 dependencies = [
     "attrs>=23.1.0",
-    "typing-extensions>=4.1.0, !=4.6.3; python_version < '3.11'",
+    "typing-extensions @ git+https://github.com/python/typing_extensions.git",
     "exceptiongroup>=1.1.1; python_version < '3.11'",
 ]
 requires-python = ">=3.8"
@@ -148,6 +148,9 @@ ignore = [
     "DTZ006", # datetimes in tests
 ]
 
+[tool.hatch.metadata]
+allow-direct-references = true
+
 [tool.hatch.version]

And then re-running pdm install -d -G :all and tox. The tests all passed.

I didn't add an entry to HISTORY.md, as it looks like cattrs's PEP-696 support is unreleased right now (so it's a bugfix for an unreleased feature)!

Copy link

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

This matches my understanding (I implemented this PEP in CPython)

@Tinche
Copy link
Member

Tinche commented May 12, 2024

Nice, thanks a lot!

@Tinche Tinche merged commit 17a7866 into python-attrs:main May 12, 2024
8 checks passed
@AlexWaygood AlexWaygood deleted the tvar-default-fix branch May 12, 2024 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants