Merge pull request #6859 from nicoddemus/kw-mapping-attr-typing

Use attrs in KeywordMapping
This commit is contained in:
Bruno Oliveira 2020-03-05 08:47:22 -03:00 committed by GitHub
commit 19bb2c6235
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 6 deletions

View File

@ -3,11 +3,16 @@ this is a place where we put datastructures used by legacy apis
we hope to remove we hope to remove
""" """
import keyword import keyword
from typing import Set
import attr import attr
from _pytest.compat import TYPE_CHECKING
from _pytest.config import UsageError from _pytest.config import UsageError
if TYPE_CHECKING:
from _pytest.nodes import Item # noqa: F401 (used in type string)
@attr.s @attr.s
class MarkMapping: class MarkMapping:
@ -25,16 +30,16 @@ class MarkMapping:
return name in self.own_mark_names return name in self.own_mark_names
@attr.s
class KeywordMapping: class KeywordMapping:
"""Provides a local mapping for keywords. """Provides a local mapping for keywords.
Given a list of names, map any substring of one of these names to True. Given a list of names, map any substring of one of these names to True.
""" """
def __init__(self, names): _names = attr.ib(type=Set[str])
self._names = names
@classmethod @classmethod
def from_item(cls, item): def from_item(cls, item: "Item") -> "KeywordMapping":
mapped_names = set() mapped_names = set()
# Add the names of the current item and any parent items # Add the names of the current item and any parent items
@ -48,15 +53,16 @@ class KeywordMapping:
mapped_names.update(item.listextrakeywords()) mapped_names.update(item.listextrakeywords())
# Add the names attached to the current function through direct assignment # Add the names attached to the current function through direct assignment
if hasattr(item, "function"): function_obj = getattr(item, "function", None)
mapped_names.update(item.function.__dict__) if function_obj:
mapped_names.update(function_obj.__dict__)
# add the markers to the keywords as we no longer handle them correctly # add the markers to the keywords as we no longer handle them correctly
mapped_names.update(mark.name for mark in item.iter_markers()) mapped_names.update(mark.name for mark in item.iter_markers())
return cls(mapped_names) return cls(mapped_names)
def __getitem__(self, subname): def __getitem__(self, subname: str) -> bool:
"""Return whether subname is included within stored names. """Return whether subname is included within stored names.
The string inclusion check is case-insensitive. The string inclusion check is case-insensitive.