Last active
May 1, 2025 13:58
-
-
Save Kos/75728458b705f89d481b3afcb89c426f to your computer and use it in GitHub Desktop.
Pytest helpers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AnyUUID: | |
def __eq__(self, other): | |
if isinstance(other, str): | |
try: | |
UUID(other) | |
return True | |
except ValueError: | |
return False | |
else: | |
return isinstance(other, UUID) | |
def __repr__(self): | |
return "AnyUUID()" | |
class AnyDate: | |
def __eq__(self, other): | |
if isinstance(other, str): | |
try: | |
datetime.fromisoformat(other) | |
return True | |
except ValueError: | |
return False | |
else: | |
return isinstance(other, datetime) | |
def __repr__(self): | |
return "AnyDate()" | |
class AnyStr: | |
def __init__(self, contains: str | None = None): | |
self.contains = contains | |
def __eq__(self, other): | |
if not isinstance(other, str): | |
return False | |
if self.contains: | |
return self.contains in other | |
return True | |
def __repr__(self): | |
if self.contains: | |
return f"AnyStr(contains={self.contains})" | |
return "AnyStr()" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment