Skip to content

Commit

Permalink
Use pattern-matching for some typing.py internals
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed May 11, 2024
1 parent b88889e commit 3ae8a3d
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 102 deletions.
20 changes: 12 additions & 8 deletions Lib/_collections_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ def __getitem__(self, item):
new_args = (t_args, t_result)
return _CallableGenericAlias(Callable, tuple(new_args))


def _is_param_expr(obj):
"""Checks if obj matches either a list of types, ``...``, ``ParamSpec`` or
``_ConcatenateGenericAlias`` from typing.py
Expand All @@ -520,22 +521,25 @@ def _is_param_expr(obj):
names = ('ParamSpec', '_ConcatenateGenericAlias')
return obj.__module__ == 'typing' and any(obj.__name__ == name for name in names)


def _type_repr(obj):
"""Return the repr() of an object, special-casing types (internal helper).
Copied from :mod:`typing` since collections.abc
shouldn't depend on that module.
(Keep this roughly in sync with the typing version.)
"""
if isinstance(obj, type):
if obj.__module__ == 'builtins':
match obj:
case type(__module__="builtins"):
return obj.__qualname__
return f'{obj.__module__}.{obj.__qualname__}'
if obj is Ellipsis:
return '...'
if isinstance(obj, FunctionType):
return obj.__name__
return repr(obj)
case type():
return f'{obj.__module__}.{obj.__qualname__}'
case EllipsisType():
return '...'
case FunctionType():
return obj.__name__
case _:
return repr(obj)


class Callable(metaclass=ABCMeta):
Expand Down
Loading

0 comments on commit 3ae8a3d

Please sign in to comment.