mark/structures: fix NodeKeywords.{__iter__,__len__} given duplicates

Regressed in c4b9b14a4 -- the `set` stuff had a reason which I had
missed.
This commit is contained in:
Ran Benita 2021-10-06 10:53:34 +03:00
parent c9267af3bf
commit cf4495ffe1
2 changed files with 26 additions and 5 deletions

View File

@ -579,14 +579,17 @@ class NodeKeywords(MutableMapping[str, Any]):
raise ValueError("cannot delete key in keywords dict")
def __iter__(self) -> Iterator[str]:
# Doesn't need to be fast.
yield from self._markers
if self.parent is not None:
yield from self.parent.keywords
for keyword in self.parent.keywords:
# self._marks and self.parent.keywords can have duplicates.
if keyword not in self._markers:
yield keyword
def __len__(self) -> int:
return len(self._markers) + (
len(self.parent.keywords) if self.parent is not None else 0
)
# Doesn't need to be fast.
return sum(1 for keyword in self)
def __repr__(self) -> str:
return f"<NodeKeywords for node {self.node}>"

View File

@ -793,7 +793,7 @@ def test_matchnodes_two_collections_same_file(pytester: Pytester) -> None:
res.stdout.fnmatch_lines(["*1 passed*"])
class TestNodekeywords:
class TestNodeKeywords:
def test_no_under(self, pytester: Pytester) -> None:
modcol = pytester.getmodulecol(
"""
@ -859,6 +859,24 @@ class TestNodekeywords:
reprec = pytester.inline_run("-k " + expression)
reprec.assertoutcome(passed=num_matching_tests, failed=0)
def test_duplicates_handled_correctly(self, pytester: Pytester) -> None:
item = pytester.getitem(
"""
import pytest
pytestmark = pytest.mark.kw
class TestClass:
pytestmark = pytest.mark.kw
def test_method(self): pass
test_method.kw = 'method'
""",
"test_method",
)
assert item.parent is not None and item.parent.parent is not None
item.parent.parent.keywords["kw"] = "class"
assert item.keywords["kw"] == "method"
assert len(item.keywords) == len(set(item.keywords))
COLLECTION_ERROR_PY_FILES = dict(
test_01_failure="""