Add helper function to MarkGenerator to verify a mark is registered

This commit is contained in:
Max Berkowitz 2024-04-25 23:15:38 -04:00 committed by GitHub
parent 9f7770efe3
commit 048abb60bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 6 deletions

View File

@ -520,11 +520,7 @@ class MarkGenerator:
self._config: Optional[Config] = None self._config: Optional[Config] = None
self._markers: Set[str] = set() self._markers: Set[str] = set()
def __getattr__(self, name: str) -> MarkDecorator: def verify_mark(self, name: str) -> None:
"""Generate a new :class:`MarkDecorator` with the given name."""
if name[0] == "_":
raise AttributeError("Marker name must NOT start with underscore")
if self._config is not None: if self._config is not None:
# We store a set of markers as a performance optimisation - if a mark # We store a set of markers as a performance optimisation - if a mark
# name is in the set we definitely know it, but a mark may be known and # name is in the set we definitely know it, but a mark may be known and
@ -556,9 +552,16 @@ class MarkGenerator:
"custom marks to avoid this warning - for details, see " "custom marks to avoid this warning - for details, see "
"https://docs.pytest.org/en/stable/how-to/mark.html" % name, "https://docs.pytest.org/en/stable/how-to/mark.html" % name,
PytestUnknownMarkWarning, PytestUnknownMarkWarning,
2, 3,
) )
def __getattr__(self, name: str) -> MarkDecorator:
"""Generate a new :class:`MarkDecorator` with the given name."""
if name[0] == "_":
raise AttributeError("Marker name must NOT start with underscore")
self.verify_mark(name)
return MarkDecorator(Mark(name, (), {}, _ispytest=True), _ispytest=True) return MarkDecorator(Mark(name, (), {}, _ispytest=True), _ispytest=True)