Skip to content

Commit

Permalink
Type-hinting for looping over the xml children (#137)
Browse files Browse the repository at this point in the history
* checks against `None` should use the `is` keyword

Checking for `None` should be done explicitly and not by relying on the falsienes of it.
This is faster and avoids problems if falsy objects are passed in as an argument.

* add typehints to `__iter__` methods

this fixes the elements being detected as type `Any` and users not getting intellisense
  • Loading branch information
Cube707 authored Jan 13, 2025
1 parent 5372679 commit 88b4595
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions junitparser/junitparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"""
import itertools
from copy import deepcopy
from typing import List, Union
from typing import List, Union, Iterator

try:
from lxml import etree
Expand Down Expand Up @@ -328,7 +328,7 @@ def __init__(self, name: str = None, classname: str = None, time: float = None):
def __hash__(self):
return super().__hash__()

def __iter__(self):
def __iter__(self) -> Iterator[Union[Result, System]]:
all_types = set.union(POSSIBLE_RESULTS, {SystemOut}, {SystemErr})
for elem in self._elem.iter():
for entry_type in all_types:
Expand Down Expand Up @@ -454,7 +454,7 @@ def __init__(self):
def add_property(self, property_: Property):
self.append(property_)

def __iter__(self):
def __iter__(self) -> Iterator[Property]:
return super().iterchildren(Property)

def __eq__(self, other):
Expand Down Expand Up @@ -500,7 +500,7 @@ def __init__(self, name=None):
self.name = name
self.filepath = None

def __iter__(self):
def __iter__(self) -> Iterator[TestCase]:
return itertools.chain(
super().iterchildren(TestCase),
(case for suite in super().iterchildren(TestSuite) for case in suite),
Expand Down Expand Up @@ -674,7 +674,7 @@ def __init__(self, name=None):
self.filepath = None
self.name = name

def __iter__(self):
def __iter__(self) -> Iterator[TestSuite]:
return super().iterchildren(TestSuite)

def __len__(self):
Expand Down Expand Up @@ -748,7 +748,7 @@ def fromstring(cls, text: str):
@classmethod
def fromfile(cls, filepath: str, parse_func=None):
"""Initiate the object from a report file."""
if parse_func:
if parse_func is not None:
tree = parse_func(filepath)
else:
tree = etree.parse(filepath) # nosec
Expand Down
4 changes: 2 additions & 2 deletions junitparser/xunit2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"""

import itertools
from typing import List, TypeVar
from typing import List, TypeVar, Iterator
from . import junitparser

T = TypeVar("T")
Expand All @@ -30,7 +30,7 @@ class TestSuite(junitparser.TestSuite):
url = junitparser.Attr()
version = junitparser.Attr()

def __iter__(self):
def __iter__(self) -> Iterator["TestCase"]:
return itertools.chain(
super().iterchildren(TestCase),
(case for suite in super().iterchildren(TestSuite) for case in suite),
Expand Down

0 comments on commit 88b4595

Please sign in to comment.