Skip to content

Commit bc8e52c

Browse files
committed
Use attrs in KeywordMapping
Also added type hinting.
1 parent a5a8d53 commit bc8e52c

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/_pytest/mark/legacy.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
we hope to remove
44
"""
55
import keyword
6+
from typing import Set
67

78
import attr
89

10+
from _pytest.compat import TYPE_CHECKING
911
from _pytest.config import UsageError
1012

13+
if TYPE_CHECKING:
14+
from _pytest.nodes import Item # noqa: F401 (used in type string)
15+
1116

1217
@attr.s
1318
class MarkMapping:
@@ -25,16 +30,16 @@ def __getitem__(self, name):
2530
return name in self.own_mark_names
2631

2732

33+
@attr.s
2834
class KeywordMapping:
2935
"""Provides a local mapping for keywords.
3036
Given a list of names, map any substring of one of these names to True.
3137
"""
3238

33-
def __init__(self, names):
34-
self._names = names
39+
_names = attr.ib(type=Set[str])
3540

3641
@classmethod
37-
def from_item(cls, item):
42+
def from_item(cls, item: "Item") -> "KeywordMapping":
3843
mapped_names = set()
3944

4045
# Add the names of the current item and any parent items
@@ -48,15 +53,16 @@ def from_item(cls, item):
4853
mapped_names.update(item.listextrakeywords())
4954

5055
# Add the names attached to the current function through direct assignment
51-
if hasattr(item, "function"):
52-
mapped_names.update(item.function.__dict__)
56+
function_obj = getattr(item, "function", None)
57+
if function_obj:
58+
mapped_names.update(function_obj.__dict__)
5359

5460
# add the markers to the keywords as we no longer handle them correctly
5561
mapped_names.update(mark.name for mark in item.iter_markers())
5662

5763
return cls(mapped_names)
5864

59-
def __getitem__(self, subname):
65+
def __getitem__(self, subname: str) -> bool:
6066
"""Return whether subname is included within stored names.
6167
6268
The string inclusion check is case-insensitive.

0 commit comments

Comments
 (0)