Skip to content

Add better default value for TypeVar default (PEP 696) #85

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 1 commit into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Release 4.4.1 (?)
Copy link
Member

Choose a reason for hiding this comment

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

Might end up being 4.5.0, but we can change that later.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We should probably just use UNRELEASED in the future.


- Add better default value for TypeVar `default` parameter, PEP 696. Enables
runtime check if `None` was passed as default. Patch by Marc Mueller (@cdce8p).

# Release 4.4.0 (October 6, 2022)

- Add `typing_extensions.Any` a backport of python 3.11's Any class which is
Expand Down
6 changes: 6 additions & 0 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3439,6 +3439,12 @@ def test_typevar(self):
class A(Generic[T]): ...
Alias = Optional[T]

def test_typevar_none(self):
U = typing_extensions.TypeVar('U')
U_None = typing_extensions.TypeVar('U_None', default=None)
self.assertEqual(U.__default__, None)
self.assertEqual(U_None.__default__, type(None))

def test_paramspec(self):
P = ParamSpec('P', default=(str, int))
self.assertEqual(P.__default__, (str, int))
Expand Down
12 changes: 6 additions & 6 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ def __init__(self, default):
if isinstance(default, (tuple, list)):
self.__default__ = tuple((typing._type_check(d, "Default must be a type")
for d in default))
elif default:
elif default != _marker:
self.__default__ = typing._type_check(default, "Default must be a type")
else:
self.__default__ = None
Expand All @@ -1171,7 +1171,7 @@ class TypeVar(typing.TypeVar, _DefaultMixin, _root=True):

def __init__(self, name, *constraints, bound=None,
covariant=False, contravariant=False,
default=None, infer_variance=False):
default=_marker, infer_variance=False):
super().__init__(name, *constraints, bound=bound, covariant=covariant,
contravariant=contravariant)
_DefaultMixin.__init__(self, default)
Expand Down Expand Up @@ -1258,7 +1258,7 @@ class ParamSpec(typing.ParamSpec, _DefaultMixin, _root=True):
__module__ = 'typing'

def __init__(self, name, *, bound=None, covariant=False, contravariant=False,
default=None):
default=_marker):
super().__init__(name, bound=bound, covariant=covariant,
contravariant=contravariant)
_DefaultMixin.__init__(self, default)
Expand Down Expand Up @@ -1334,7 +1334,7 @@ def kwargs(self):
return ParamSpecKwargs(self)

def __init__(self, name, *, bound=None, covariant=False, contravariant=False,
default=None):
default=_marker):
super().__init__([self])
self.__name__ = name
self.__covariant__ = bool(covariant)
Expand Down Expand Up @@ -1850,7 +1850,7 @@ def _is_unpack(obj):
class TypeVarTuple(typing.TypeVarTuple, _DefaultMixin, _root=True):
"""Type variable tuple."""

def __init__(self, name, *, default=None):
def __init__(self, name, *, default=_marker):
super().__init__(name)
_DefaultMixin.__init__(self, default)

Expand Down Expand Up @@ -1913,7 +1913,7 @@ def get_shape(self) -> Tuple[*Ts]:
def __iter__(self):
yield self.__unpacked__

def __init__(self, name, *, default=None):
def __init__(self, name, *, default=_marker):
self.__name__ = name
_DefaultMixin.__init__(self, default)

Expand Down