3
3
we hope to remove
4
4
"""
5
5
import keyword
6
+ from typing import Set
6
7
7
8
import attr
8
9
10
+ from _pytest .compat import TYPE_CHECKING
9
11
from _pytest .config import UsageError
10
12
13
+ if TYPE_CHECKING :
14
+ from _pytest .nodes import Item # noqa: F401 (used in type string)
15
+
11
16
12
17
@attr .s
13
18
class MarkMapping :
@@ -25,16 +30,16 @@ def __getitem__(self, name):
25
30
return name in self .own_mark_names
26
31
27
32
33
+ @attr .s
28
34
class KeywordMapping :
29
35
"""Provides a local mapping for keywords.
30
36
Given a list of names, map any substring of one of these names to True.
31
37
"""
32
38
33
- def __init__ (self , names ):
34
- self ._names = names
39
+ _names = attr .ib (type = Set [str ])
35
40
36
41
@classmethod
37
- def from_item (cls , item ) :
42
+ def from_item (cls , item : "Item" ) -> "KeywordMapping" :
38
43
mapped_names = set ()
39
44
40
45
# Add the names of the current item and any parent items
@@ -48,15 +53,16 @@ def from_item(cls, item):
48
53
mapped_names .update (item .listextrakeywords ())
49
54
50
55
# 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__ )
53
59
54
60
# add the markers to the keywords as we no longer handle them correctly
55
61
mapped_names .update (mark .name for mark in item .iter_markers ())
56
62
57
63
return cls (mapped_names )
58
64
59
- def __getitem__ (self , subname ) :
65
+ def __getitem__ (self , subname : str ) -> bool :
60
66
"""Return whether subname is included within stored names.
61
67
62
68
The string inclusion check is case-insensitive.
0 commit comments