Skip to content

Commit baa3ab2

Browse files
committed
TYP: Type read_xml and adjust type hints
1 parent 9512393 commit baa3ab2

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

pandas/_typing.py

+1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ def closed(self) -> bool:
246246
CompressionOptions = Optional[
247247
Union[Literal["infer", "gzip", "bz2", "zip", "xz", "zstd"], CompressionDict]
248248
]
249+
XMLParsers = Literal["lxml", "etree"]
249250

250251

251252
# types in DataFrameFormatter

pandas/io/xml.py

+29-30
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
from __future__ import annotations
66

77
import io
8+
from typing import Iterable
89

910
from pandas._typing import (
1011
CompressionOptions,
1112
FilePath,
1213
ReadBuffer,
1314
StorageOptions,
15+
XMLParsers,
1416
)
1517
from pandas.compat._optional import import_optional_dependency
1618
from pandas.errors import (
@@ -98,17 +100,17 @@ class _XMLFrameParser:
98100

99101
def __init__(
100102
self,
101-
path_or_buffer,
102-
xpath,
103-
namespaces,
104-
elems_only,
105-
attrs_only,
106-
names,
107-
encoding,
108-
stylesheet,
103+
path_or_buffer: FilePath | ReadBuffer[bytes] | ReadBuffer[str],
104+
xpath: str,
105+
namespaces: dict | list[dict] | None,
106+
elems_only: bool,
107+
attrs_only: bool,
108+
names: Iterable[str] | None,
109+
encoding: str | None,
110+
stylesheet: FilePath | ReadBuffer[bytes] | ReadBuffer[str] | None,
109111
compression: CompressionOptions,
110112
storage_options: StorageOptions,
111-
) -> None:
113+
):
112114
self.path_or_buffer = path_or_buffer
113115
self.xpath = xpath
114116
self.namespaces = namespaces
@@ -371,9 +373,6 @@ class _LxmlFrameParser(_XMLFrameParser):
371373
XPath 1.0 and XSLT 1.0.
372374
"""
373375

374-
def __init__(self, *args, **kwargs) -> None:
375-
super().__init__(*args, **kwargs)
376-
377376
def parse_data(self) -> list[dict[str, str | None]]:
378377
"""
379378
Parse xml data.
@@ -570,7 +569,7 @@ def _transform_doc(self) -> bytes:
570569

571570
def get_data_from_filepath(
572571
filepath_or_buffer: FilePath | bytes | ReadBuffer[bytes] | ReadBuffer[str],
573-
encoding,
572+
encoding: str | None,
574573
compression: CompressionOptions,
575574
storage_options: StorageOptions,
576575
) -> str | bytes | ReadBuffer[bytes] | ReadBuffer[str]:
@@ -658,15 +657,15 @@ class that build Data Frame and infers specific dtypes.
658657

659658

660659
def _parse(
661-
path_or_buffer,
662-
xpath,
663-
namespaces,
664-
elems_only,
665-
attrs_only,
666-
names,
667-
encoding,
668-
parser,
669-
stylesheet,
660+
path_or_buffer: FilePath | ReadBuffer[bytes] | ReadBuffer[str],
661+
xpath: str,
662+
namespaces: dict | list[dict] | None,
663+
elems_only: bool,
664+
attrs_only: bool,
665+
names: Iterable[str] | None,
666+
encoding: str | None,
667+
parser: XMLParsers,
668+
stylesheet: FilePath | ReadBuffer[bytes] | ReadBuffer[str] | None,
670669
compression: CompressionOptions,
671670
storage_options: StorageOptions,
672671
**kwargs,
@@ -686,11 +685,11 @@ def _parse(
686685
* If parser is not lxml or etree.
687686
"""
688687

689-
lxml = import_optional_dependency("lxml.etree", errors="ignore")
690-
691688
p: _EtreeFrameParser | _LxmlFrameParser
692689

693690
if parser == "lxml":
691+
lxml = import_optional_dependency("lxml.etree", errors="ignore")
692+
694693
if lxml is not None:
695694
p = _LxmlFrameParser(
696695
path_or_buffer,
@@ -734,13 +733,13 @@ def _parse(
734733
)
735734
def read_xml(
736735
path_or_buffer: FilePath | ReadBuffer[bytes] | ReadBuffer[str],
737-
xpath: str | None = "./*",
736+
xpath: str = "./*",
738737
namespaces: dict | list[dict] | None = None,
739-
elems_only: bool | None = False,
740-
attrs_only: bool | None = False,
741-
names: list[str] | None = None,
738+
elems_only: bool = False,
739+
attrs_only: bool = False,
740+
names: Iterable[str] | None = None,
742741
encoding: str | None = "utf-8",
743-
parser: str | None = "lxml",
742+
parser: XMLParsers = "lxml",
744743
stylesheet: FilePath | ReadBuffer[bytes] | ReadBuffer[str] | None = None,
745744
compression: CompressionOptions = "infer",
746745
storage_options: StorageOptions = None,
@@ -765,7 +764,7 @@ def read_xml(
765764
expressions. For more complex XPath, use ``lxml`` which requires
766765
installation.
767766
768-
namespaces : dict, optional
767+
namespaces : dict, list of dicts, optional
769768
The namespaces defined in XML document as dicts with key being
770769
namespace prefix and value the URI. There is no need to include all
771770
namespaces in XML, only the ones used in ``xpath`` expression.

0 commit comments

Comments
 (0)