Skip to content

Commit 198aaaa

Browse files
Special-case all built-in names in BaseAutoMatcher.
1 parent d33fdfb commit 198aaaa

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

tests/utils/matchers/meta.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import builtins
2+
import types
13
from typing import Any, get_args, get_origin
24

35
from hamcrest import anything
@@ -7,6 +9,8 @@
79
from hamcrest.core.helpers.wrap_matcher import wrap_matcher
810
from hamcrest.core.matcher import Matcher
911

12+
BUILTINS = {name for name in dir(builtins) if isinstance(getattr(builtins, name), (types.BuiltinFunctionType, type))}
13+
1014

1115
class AutoMatcherMeta(type):
1216
def __new__(cls, name, bases, namespace, **_kwargs):
@@ -32,7 +36,7 @@ def __new__(cls, name, bases, namespace, **_kwargs):
3236
raise TypeError(msg)
3337

3438
for field_name in domain_class.__annotations__:
35-
attr_name = f"{field_name}_" if field_name in {"id", "type"} else field_name
39+
attr_name = f"{field_name}_" if field_name in BUILTINS else field_name
3640
namespace[attr_name] = anything()
3741

3842
return super().__new__(cls, name, bases, namespace)
@@ -69,33 +73,33 @@ def is_status() -> Matcher[Status]: return StatusMatcher()
6973
def describe_to(self, description: Description) -> None:
7074
description.append_text(f"{self.__domain_class__.__name__} with")
7175
for field_name in self.__domain_class__.__annotations__:
72-
attr_name = f"{field_name}_" if field_name in {"id", "type"} else field_name
76+
attr_name = f"{field_name}_" if field_name in BUILTINS else field_name
7377
self.append_matcher_description(getattr(self, attr_name), field_name, description)
7478

7579
def _matches(self, item: T) -> bool:
7680
return all(
77-
getattr(self, f"{field}_" if field in {"id", "type"} else field).matches(getattr(item, field))
81+
getattr(self, f"{field}_" if field in BUILTINS else field).matches(getattr(item, field))
7882
for field in self.__domain_class__.__annotations__
7983
)
8084

8185
def describe_mismatch(self, item: T, mismatch_description: Description) -> None:
8286
mismatch_description.append_text(f"was {self.__domain_class__.__name__} with")
8387
for field_name in self.__domain_class__.__annotations__:
84-
matcher = getattr(self, f"{field_name}_" if field_name in {"id", "type"} else field_name)
88+
matcher = getattr(self, f"{field_name}_" if field_name in BUILTINS else field_name)
8589
value = getattr(item, field_name)
8690
self.describe_field_mismatch(matcher, field_name, value, mismatch_description)
8791

8892
def describe_match(self, item: T, match_description: Description) -> None:
8993
match_description.append_text(f"was {self.__domain_class__.__name__} with")
9094
for field_name in self.__domain_class__.__annotations__:
91-
matcher = getattr(self, f"{field_name}_" if field_name in {"id", "type"} else field_name)
95+
matcher = getattr(self, f"{field_name}_" if field_name in BUILTINS else field_name)
9296
value = getattr(item, field_name)
9397
self.describe_field_match(matcher, field_name, value, match_description)
9498

9599
def __getattr__(self, name: str):
96100
if name.startswith(("with_", "and_")):
97101
base = name.removeprefix("with_").removeprefix("and_")
98-
attr = f"{base}_" if base in {"id", "type"} else base
102+
attr = f"{base}_" if base in BUILTINS else base
99103
if hasattr(self, attr):
100104

101105
def setter(value):
@@ -109,7 +113,7 @@ def setter(value):
109113
def __dir__(self):
110114
dynamic_methods = []
111115
for field_name in self.__domain_class__.__annotations__:
112-
base = field_name.rstrip("_") if field_name in {"id", "type"} else field_name
116+
base = field_name.rstrip("_") if field_name in BUILTINS else field_name
113117
dynamic_methods.extend([f"with_{base}", f"and_{base}"])
114118
return list(super().__dir__()) + dynamic_methods
115119

0 commit comments

Comments
 (0)