Merge pull request #9168 from bluetech/node-keywords-dups
mark/structures: fix NodeKeywords.{__iter__,__len__} given duplicates
			
			
This commit is contained in:
		
						commit
						e077f1cbd5
					
				| 
						 | 
					@ -579,14 +579,17 @@ class NodeKeywords(MutableMapping[str, Any]):
 | 
				
			||||||
        raise ValueError("cannot delete key in keywords dict")
 | 
					        raise ValueError("cannot delete key in keywords dict")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __iter__(self) -> Iterator[str]:
 | 
					    def __iter__(self) -> Iterator[str]:
 | 
				
			||||||
 | 
					        # Doesn't need to be fast.
 | 
				
			||||||
        yield from self._markers
 | 
					        yield from self._markers
 | 
				
			||||||
        if self.parent is not None:
 | 
					        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:
 | 
					    def __len__(self) -> int:
 | 
				
			||||||
        return len(self._markers) + (
 | 
					        # Doesn't need to be fast.
 | 
				
			||||||
            len(self.parent.keywords) if self.parent is not None else 0
 | 
					        return sum(1 for keyword in self)
 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __repr__(self) -> str:
 | 
					    def __repr__(self) -> str:
 | 
				
			||||||
        return f"<NodeKeywords for node {self.node}>"
 | 
					        return f"<NodeKeywords for node {self.node}>"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -793,7 +793,7 @@ def test_matchnodes_two_collections_same_file(pytester: Pytester) -> None:
 | 
				
			||||||
    res.stdout.fnmatch_lines(["*1 passed*"])
 | 
					    res.stdout.fnmatch_lines(["*1 passed*"])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TestNodekeywords:
 | 
					class TestNodeKeywords:
 | 
				
			||||||
    def test_no_under(self, pytester: Pytester) -> None:
 | 
					    def test_no_under(self, pytester: Pytester) -> None:
 | 
				
			||||||
        modcol = pytester.getmodulecol(
 | 
					        modcol = pytester.getmodulecol(
 | 
				
			||||||
            """
 | 
					            """
 | 
				
			||||||
| 
						 | 
					@ -859,6 +859,24 @@ class TestNodekeywords:
 | 
				
			||||||
            reprec = pytester.inline_run("-k " + expression)
 | 
					            reprec = pytester.inline_run("-k " + expression)
 | 
				
			||||||
            reprec.assertoutcome(passed=num_matching_tests, failed=0)
 | 
					            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(
 | 
					COLLECTION_ERROR_PY_FILES = dict(
 | 
				
			||||||
    test_01_failure="""
 | 
					    test_01_failure="""
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue