Skip to content

Commit

Permalink
Small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
erezsh committed Mar 16, 2024
1 parent 137bbea commit 6bdd480
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/dataclass.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Configuration
-------------

.. autoclass:: runtype.dataclass.Configuration
:members: ensure_isa, cast, canonize_type, on_default
:members: ensure_isa, cast, to_canonical_type, on_default


.. autoclass:: runtype.dataclass.PythonConfiguration
Expand Down
2 changes: 1 addition & 1 deletion runtype/typesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def isinstance(self, obj, t: type) -> bool:
def issubclass(self, t1: type, t2: type) -> bool:
raise NotImplementedError()

def canonize_type(self, t: type) -> type:
def to_canonical_type(self, t: type) -> type:
return t

def get_type(self, obj) -> type:
Expand Down
2 changes: 1 addition & 1 deletion runtype/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get_func_signatures(typesystem, f):
t = typesystem.default_type
else:
# Canonize to detect more collisions on construction, instead of during dispatch
t = typesystem.canonize_type(t)
t = typesystem.to_canonical_type(t)

if p.default is not p.empty:
# From now on, everything is optional
Expand Down
8 changes: 4 additions & 4 deletions runtype/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def assert_isa(obj, t):
raise TypeError(msg)


_CANONIZED_TYPES = {
_CANONICAL_TYPES = {
# Any: object,
List: list,
Set: set,
Expand All @@ -68,9 +68,9 @@ def assert_isa(obj, t):
Tuple[Any, ...]: tuple,
}

def canonize_type(t):
def to_canonical_type(t):
"Turns List -> list, Dict -> dict, etc."
return _CANONIZED_TYPES.get(t, t)
return _CANONICAL_TYPES.get(t, t)


def issubclass(t1, t2):
Expand All @@ -90,7 +90,7 @@ def issubclass(t1, t2):
class PythonTyping(TypeSystem):
isinstance = staticmethod(isa)
issubclass = staticmethod(issubclass)
canonize_type = staticmethod(canonize_type)
to_canonical_type = staticmethod(to_canonical_type)
get_type = type
default_type = object

Expand Down
2 changes: 1 addition & 1 deletion tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def test_constraint(self):
def test_typesystem(self):
t = TypeSystem()
o = object()
assert t.canonize_type(o) is o
assert t.to_canonical_type(o) is o

class IntOrder(TypeSystem):
def issubclass(self, a, b):
Expand Down

0 comments on commit 6bdd480

Please sign in to comment.