Simplify existing type hints.

pull/77203/head
Matt Clay 3 years ago
parent ccdb552d90
commit 871b2ca73a

@ -15,7 +15,6 @@ import stat
import tarfile import tarfile
import time import time
import threading import threading
import typing as t
from urllib.error import HTTPError from urllib.error import HTTPError
from urllib.parse import quote as urlquote, urlencode, urlparse, parse_qs, urljoin from urllib.parse import quote as urlquote, urlencode, urlparse, parse_qs, urljoin
@ -299,7 +298,7 @@ class GalaxyAPI:
return to_native(self.name) return to_native(self.name)
def __unicode__(self): def __unicode__(self):
# type: (GalaxyAPI) -> t.Text # type: (GalaxyAPI) -> str
"""Render GalaxyAPI as a unicode/text string representation.""" """Render GalaxyAPI as a unicode/text string representation."""
return to_text(self.name) return to_text(self.name)
@ -315,7 +314,7 @@ class GalaxyAPI:
) )
def __lt__(self, other_galaxy_api): def __lt__(self, other_galaxy_api):
# type: (GalaxyAPI, GalaxyAPI) -> t.Union[bool, 'NotImplemented'] # type: (GalaxyAPI, GalaxyAPI) -> bool | NotImplemented
"""Return whether the instance priority is higher than other.""" """Return whether the instance priority is higher than other."""
if not isinstance(other_galaxy_api, self.__class__): if not isinstance(other_galaxy_api, self.__class__):
return NotImplemented return NotImplemented

@ -20,8 +20,6 @@ __metaclass__ = type
from os.path import basename from os.path import basename
import typing as t
import ansible.constants as C import ansible.constants as C
from ansible.errors import AnsibleParserError from ansible.errors import AnsibleParserError
from ansible.playbook.attribute import FieldAttribute from ansible.playbook.attribute import FieldAttribute
@ -45,9 +43,9 @@ class IncludeRole(TaskInclude):
circumstances related to the `- include_role: ...` circumstances related to the `- include_role: ...`
""" """
BASE = ('name', 'role') # type: t.Tuple[str, ...] # directly assigned BASE = ('name', 'role') # type: tuple[str, ...] # directly assigned
FROM_ARGS = ('tasks_from', 'vars_from', 'defaults_from', 'handlers_from') # type: t.Tuple[str, ...] # used to populate from dict in role FROM_ARGS = ('tasks_from', 'vars_from', 'defaults_from', 'handlers_from') # type: tuple[str, ...] # used to populate from dict in role
OTHER_ARGS = ('apply', 'public', 'allow_duplicates', 'rolespec_validate') # type: t.Tuple[str, ...] # assigned to matching property OTHER_ARGS = ('apply', 'public', 'allow_duplicates', 'rolespec_validate') # type: tuple[str, ...] # assigned to matching property
VALID_ARGS = tuple(frozenset(BASE + FROM_ARGS + OTHER_ARGS)) # all valid args VALID_ARGS = tuple(frozenset(BASE + FROM_ARGS + OTHER_ARGS)) # all valid args
# ================================================================================= # =================================================================================

@ -8,7 +8,6 @@ import abc
import dataclasses import dataclasses
import datetime import datetime
import decimal import decimal
import typing as t
from xml.dom import minidom from xml.dom import minidom
# noinspection PyPep8Naming # noinspection PyPep8Naming
@ -18,9 +17,9 @@ from xml.etree import ElementTree as ET
@dataclasses.dataclass @dataclasses.dataclass
class TestResult(metaclass=abc.ABCMeta): class TestResult(metaclass=abc.ABCMeta):
"""Base class for the result of a test case.""" """Base class for the result of a test case."""
output: t.Optional[str] = None output: str | None = None
message: t.Optional[str] = None message: str | None = None
type: t.Optional[str] = None type: str | None = None
def __post_init__(self): def __post_init__(self):
if self.type is None: if self.type is None:
@ -31,7 +30,7 @@ class TestResult(metaclass=abc.ABCMeta):
def tag(self) -> str: def tag(self) -> str:
"""Tag name for the XML element created by this result type.""" """Tag name for the XML element created by this result type."""
def get_attributes(self) -> t.Dict[str, str]: def get_attributes(self) -> dict[str, str]:
"""Return a dictionary of attributes for this instance.""" """Return a dictionary of attributes for this instance."""
return _attributes( return _attributes(
message=self.message, message=self.message,
@ -68,16 +67,16 @@ class TestError(TestResult):
class TestCase: class TestCase:
"""An individual test case.""" """An individual test case."""
name: str name: str
assertions: t.Optional[int] = None assertions: int | None = None
classname: t.Optional[str] = None classname: str | None = None
status: t.Optional[str] = None status: str | None = None
time: t.Optional[decimal.Decimal] = None time: decimal.Decimal | None = None
errors: t.List[TestError] = dataclasses.field(default_factory=list) errors: list[TestError] = dataclasses.field(default_factory=list)
failures: t.List[TestFailure] = dataclasses.field(default_factory=list) failures: list[TestFailure] = dataclasses.field(default_factory=list)
skipped: t.Optional[str] = None skipped: str | None = None
system_out: t.Optional[str] = None system_out: str | None = None
system_err: t.Optional[str] = None system_err: str | None = None
is_disabled: bool = False is_disabled: bool = False
@ -96,7 +95,7 @@ class TestCase:
"""True if the test case was skipped.""" """True if the test case was skipped."""
return bool(self.skipped) return bool(self.skipped)
def get_attributes(self) -> t.Dict[str, str]: def get_attributes(self) -> dict[str, str]:
"""Return a dictionary of attributes for this instance.""" """Return a dictionary of attributes for this instance."""
return _attributes( return _attributes(
assertions=self.assertions, assertions=self.assertions,
@ -129,15 +128,15 @@ class TestCase:
class TestSuite: class TestSuite:
"""A collection of test cases.""" """A collection of test cases."""
name: str name: str
hostname: t.Optional[str] = None hostname: str | None = None
id: t.Optional[str] = None id: str | None = None
package: t.Optional[str] = None package: str | None = None
timestamp: t.Optional[datetime.datetime] = None timestamp: datetime.datetime | None = None
properties: t.Dict[str, str] = dataclasses.field(default_factory=dict) properties: dict[str, str] = dataclasses.field(default_factory=dict)
cases: t.List[TestCase] = dataclasses.field(default_factory=list) cases: list[TestCase] = dataclasses.field(default_factory=list)
system_out: t.Optional[str] = None system_out: str | None = None
system_err: t.Optional[str] = None system_err: str | None = None
@property @property
def disabled(self) -> int: def disabled(self) -> int:
@ -167,9 +166,9 @@ class TestSuite:
@property @property
def time(self) -> decimal.Decimal: def time(self) -> decimal.Decimal:
"""The total time from all test cases.""" """The total time from all test cases."""
return t.cast(decimal.Decimal, sum(case.time for case in self.cases if case.time)) return decimal.Decimal(sum(case.time for case in self.cases if case.time))
def get_attributes(self) -> t.Dict[str, str]: def get_attributes(self) -> dict[str, str]:
"""Return a dictionary of attributes for this instance.""" """Return a dictionary of attributes for this instance."""
return _attributes( return _attributes(
disabled=self.disabled, disabled=self.disabled,
@ -206,9 +205,9 @@ class TestSuite:
@dataclasses.dataclass @dataclasses.dataclass
class TestSuites: class TestSuites:
"""A collection of test suites.""" """A collection of test suites."""
name: t.Optional[str] = None name: str | None = None
suites: t.List[TestSuite] = dataclasses.field(default_factory=list) suites: list[TestSuite] = dataclasses.field(default_factory=list)
@property @property
def disabled(self) -> int: def disabled(self) -> int:
@ -233,9 +232,9 @@ class TestSuites:
@property @property
def time(self) -> decimal.Decimal: def time(self) -> decimal.Decimal:
"""The total time from all test cases.""" """The total time from all test cases."""
return t.cast(decimal.Decimal, sum(suite.time for suite in self.suites)) return decimal.Decimal(sum(suite.time for suite in self.suites))
def get_attributes(self) -> t.Dict[str, str]: def get_attributes(self) -> dict[str, str]:
"""Return a dictionary of attributes for this instance.""" """Return a dictionary of attributes for this instance."""
return _attributes( return _attributes(
disabled=self.disabled, disabled=self.disabled,
@ -258,7 +257,7 @@ class TestSuites:
return _pretty_xml(self.get_xml_element()) return _pretty_xml(self.get_xml_element())
def _attributes(**kwargs) -> t.Dict[str, str]: def _attributes(**kwargs) -> dict[str, str]:
"""Return the given kwargs as a dictionary with values converted to strings. Items with a value of None will be omitted.""" """Return the given kwargs as a dictionary with values converted to strings. Items with a value of None will be omitted."""
return {key: str(value) for key, value in kwargs.items() if value is not None} return {key: str(value) for key, value in kwargs.items() if value is not None}

Loading…
Cancel
Save