Skip to content

Commit

Permalink
Upgrade typing_extensions to 4.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pfmoore committed Apr 2, 2023
1 parent ffac4e4 commit 54f6384
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 9 deletions.
1 change: 1 addition & 0 deletions news/typing_extensions.vendor.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Upgrade typing_extensions to 4.5.0
119 changes: 111 additions & 8 deletions src/pip/_vendor/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import collections
import collections.abc
import functools
import inspect
import operator
import sys
import types as _types
import typing
import warnings


__all__ = [
Expand Down Expand Up @@ -51,6 +53,7 @@
'assert_type',
'clear_overloads',
'dataclass_transform',
'deprecated',
'get_overloads',
'final',
'get_args',
Expand Down Expand Up @@ -728,6 +731,8 @@ def _typeddict_new(*args, total=True, **kwargs):
_typeddict_new.__text_signature__ = ('($cls, _typename, _fields=None,'
' /, *, total=True, **kwargs)')

_TAKES_MODULE = "module" in inspect.signature(typing._type_check).parameters

class _TypedDictMeta(type):
def __init__(cls, name, bases, ns, total=True):
super().__init__(name, bases, ns)
Expand All @@ -753,8 +758,10 @@ def __new__(cls, name, bases, ns, total=True):
annotations = {}
own_annotations = ns.get('__annotations__', {})
msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
kwds = {"module": tp_dict.__module__} if _TAKES_MODULE else {}
own_annotations = {
n: typing._type_check(tp, msg) for n, tp in own_annotations.items()
n: typing._type_check(tp, msg, **kwds)
for n, tp in own_annotations.items()
}
required_keys = set()
optional_keys = set()
Expand Down Expand Up @@ -1157,7 +1164,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 +1178,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 +1265,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 +1341,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 +1857,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 +1920,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 Expand Up @@ -1993,14 +2000,16 @@ def int_or_str(arg: int | str) -> None:
raise AssertionError("Expected code to be unreachable")


if hasattr(typing, 'dataclass_transform'):
if sys.version_info >= (3, 12):
# dataclass_transform exists in 3.11 but lacks the frozen_default parameter
dataclass_transform = typing.dataclass_transform
else:
def dataclass_transform(
*,
eq_default: bool = True,
order_default: bool = False,
kw_only_default: bool = False,
frozen_default: bool = False,
field_specifiers: typing.Tuple[
typing.Union[typing.Type[typing.Any], typing.Callable[..., typing.Any]],
...
Expand Down Expand Up @@ -2057,6 +2066,8 @@ class CustomerModel(ModelBase):
assumed to be True or False if it is omitted by the caller.
- ``kw_only_default`` indicates whether the ``kw_only`` parameter is
assumed to be True or False if it is omitted by the caller.
- ``frozen_default`` indicates whether the ``frozen`` parameter is
assumed to be True or False if it is omitted by the caller.
- ``field_specifiers`` specifies a static list of supported classes
or functions that describe fields, similar to ``dataclasses.field()``.
Expand All @@ -2071,6 +2082,7 @@ def decorator(cls_or_fn):
"eq_default": eq_default,
"order_default": order_default,
"kw_only_default": kw_only_default,
"frozen_default": frozen_default,
"field_specifiers": field_specifiers,
"kwargs": kwargs,
}
Expand Down Expand Up @@ -2102,12 +2114,103 @@ def method(self) -> None:
This helps prevent bugs that may occur when a base class is changed
without an equivalent change to a child class.
There is no runtime checking of these properties. The decorator
sets the ``__override__`` attribute to ``True`` on the decorated object
to allow runtime introspection.
See PEP 698 for details.
"""
try:
__arg.__override__ = True
except (AttributeError, TypeError):
# Skip the attribute silently if it is not writable.
# AttributeError happens if the object has __slots__ or a
# read-only property, TypeError if it's a builtin class.
pass
return __arg


if hasattr(typing, "deprecated"):
deprecated = typing.deprecated
else:
_T = typing.TypeVar("_T")

def deprecated(
__msg: str,
*,
category: typing.Optional[typing.Type[Warning]] = DeprecationWarning,
stacklevel: int = 1,
) -> typing.Callable[[_T], _T]:
"""Indicate that a class, function or overload is deprecated.
Usage:
@deprecated("Use B instead")
class A:
pass
@deprecated("Use g instead")
def f():
pass
@overload
@deprecated("int support is deprecated")
def g(x: int) -> int: ...
@overload
def g(x: str) -> int: ...
When this decorator is applied to an object, the type checker
will generate a diagnostic on usage of the deprecated object.
No runtime warning is issued. The decorator sets the ``__deprecated__``
attribute on the decorated object to the deprecation message
passed to the decorator. If applied to an overload, the decorator
must be after the ``@overload`` decorator for the attribute to
exist on the overload as returned by ``get_overloads()``.
See PEP 702 for details.
"""
def decorator(__arg: _T) -> _T:
if category is None:
__arg.__deprecated__ = __msg
return __arg
elif isinstance(__arg, type):
original_new = __arg.__new__
has_init = __arg.__init__ is not object.__init__

@functools.wraps(original_new)
def __new__(cls, *args, **kwargs):
warnings.warn(__msg, category=category, stacklevel=stacklevel + 1)
# Mirrors a similar check in object.__new__.
if not has_init and (args or kwargs):
raise TypeError(f"{cls.__name__}() takes no arguments")
if original_new is not object.__new__:
return original_new(cls, *args, **kwargs)
else:
return original_new(cls)

__arg.__new__ = staticmethod(__new__)
__arg.__deprecated__ = __new__.__deprecated__ = __msg
return __arg
elif callable(__arg):
@functools.wraps(__arg)
def wrapper(*args, **kwargs):
warnings.warn(__msg, category=category, stacklevel=stacklevel + 1)
return __arg(*args, **kwargs)

__arg.__deprecated__ = wrapper.__deprecated__ = __msg
return wrapper
else:
raise TypeError(
"@deprecated decorator with non-None category must be applied to "
f"a class or callable, not {__arg!r}"
)

return decorator


# We have to do some monkey patching to deal with the dual nature of
# Unpack/TypeVarTuple:
# - We want Unpack to be a kind of TypeVar so it gets accepted in
Expand Down
2 changes: 1 addition & 1 deletion src/pip/_vendor/vendor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ requests==2.28.2
urllib3==1.26.15
rich==13.3.3
pygments==2.13.0
typing_extensions==4.4.0
typing_extensions==4.5.0
resolvelib==1.0.1
setuptools==65.6.3
six==1.16.0
Expand Down

0 comments on commit 54f6384

Please sign in to comment.