Skip to content

Commit

Permalink
Typehint and PEP cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
rlayers committed Feb 17, 2023
1 parent 0fa3e92 commit 2723510
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pawpaw/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ def parameter_enum_not_in(cls, name: str, value: typing.Any, enum_: enum.Enum) -
def _get_type_strs(cls, *allowed) -> typing.Iterable[str]:
for t in allowed:
if hasattr(t, '__qualname__'):
if (qn := t.__qualname__) == 'Callable':
if t.__qualname__ == 'Callable':
yield str(t)
else:
yield t.__qualname__
elif hasattr(t, '__bound__'):
yield from cls._get_type_strs(t.__bound__)
elif (origin := typing.get_origin(t)) is types.UnionType:
elif typing.get_origin(t) is types.UnionType:
args = typing.get_args(t)
yield from cls._get_type_strs(*args)
elif t is None:
Expand Down
2 changes: 0 additions & 2 deletions pawpaw/nlp/nlp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import itertools
import locale
import typing

import regex
import pawpaw
Expand Down
2 changes: 1 addition & 1 deletion pawpaw/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pawpaw


# Finds indeces of non-doubled escape chars
# Finds indices of non-doubled escape chars
def find_escapes(
src: str | pawpaw.Ito,
escape: str = '\\',
Expand Down
6 changes: 3 additions & 3 deletions pawpaw/xml/xml_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import typing

import regex
from pawpaw import Ito, Types, query, xml
from pawpaw import Ito, query, xml
from pawpaw.errors import Errors
from pawpaw.arborform import Extract

Expand Down Expand Up @@ -104,7 +104,7 @@ def _query_xmlns(cls) -> query.Query:
@classmethod
def get_xmlns(cls, element: ET.Element) -> typing.Dict[QualifiedName, Ito]:
if cls._query_xmlns is None:
cls._query_xmlns = query.compile(f'*[d:{xml.descriptors.START_TAG}]/*[d:{xml.descriptors.ATTRIBUTES}]/*[d:{xml.descriptors.ATTRIBUTE}]' + '{*[p:is_xmlns]}')
cls.__query_xmlns = query.compile(f'*[d:{xml.descriptors.START_TAG}]/*[d:{xml.descriptors.ATTRIBUTES}]/*[d:{xml.descriptors.ATTRIBUTE}]' + '{*[p:is_xmlns]}')

if not isinstance(element, ET.Element):
raise Errors.parameter_invalid_type('element', element, ET.Element)
Expand All @@ -114,7 +114,7 @@ def get_xmlns(cls, element: ET.Element) -> typing.Dict[QualifiedName, Ito]:
return {
cls.get_qualified_name(xmlns): xmlns.find(f'*[d:{xml.descriptors.VALUE}]')
for xmlns
in cls._query_xmlns.find_all(element.ito, predicates=cls._query_xmlns_predicates)
in cls.__query_xmlns.find_all(element.ito, predicates=cls._query_xmlns_predicates)
}

@classmethod
Expand Down
5 changes: 2 additions & 3 deletions pawpaw/xml/xml_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
sys.modules['_elementtree'] = None
import xml.etree.ElementTree as ET
import xml.parsers.expat as expat
import itertools

import regex
from pawpaw import Span, Ito, xml, Types
from pawpaw import Span, Ito, xml
from pawpaw.arborform import Extract


Expand Down Expand Up @@ -159,7 +158,7 @@ def _extract_itos(self, element: ET.Element) -> None:
# c) could contain pi and comments
# See https://docs.python.org/3/library/xml.etree.elementtree.html for definition of .text and .tail

last_child: ET.Element = None
last_child: ET.Element | None = None
for child in element:
self._extract_itos(child)
ito.children.add(child.ito)
Expand Down

0 comments on commit 2723510

Please sign in to comment.