Skip to content

TYP: some types for pandas/core/arrays/sparse/dtype.py #29899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pandas/core/arrays/sparse/dtype.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Sparse Dtype"""

import re
from typing import Any
from typing import Any, Tuple, Union

import numpy as np

Expand Down Expand Up @@ -223,7 +223,7 @@ def construct_from_string(cls, string):
raise TypeError(msg)

@staticmethod
def _parse_subtype(dtype):
def _parse_subtype(dtype) -> Tuple[str, Union[str, bool]]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not necessarily for this PR, but the Union[str, bool] is kind of weird. maybe split into separate return values with clearer types?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can dtype be typed as str?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we could cast has_fill_value to bool before returning. cast as in the dynamic bool(...) sense not cast from typing?

"""
Parse a string to get the subtype

Expand All @@ -246,7 +246,7 @@ def _parse_subtype(dtype):
"""
xpr = re.compile(r"Sparse\[(?P<subtype>[^,]*)(, )?(?P<fill_value>.*?)?\]$")
m = xpr.match(dtype)
has_fill_value = False
has_fill_value: Union[str, bool] = False
if m:
subtype = m.groupdict()["subtype"]
has_fill_value = m.groupdict()["fill_value"] or has_fill_value
Expand Down