Skip to content

Instantly share code, notes, and snippets.

@Kos
Last active May 1, 2025 13:58
Show Gist options
  • Save Kos/75728458b705f89d481b3afcb89c426f to your computer and use it in GitHub Desktop.
Save Kos/75728458b705f89d481b3afcb89c426f to your computer and use it in GitHub Desktop.
Pytest helpers
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