Skip to content

TYP: tslib.pyi #40473

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 3 commits into from
Mar 18, 2021
Merged

TYP: tslib.pyi #40473

merged 3 commits into from
Mar 18, 2021

Conversation

jbrockmendel
Copy link
Member

  • closes #xxxx
  • tests added / passed
  • Ensure all linting tests pass, see here for how to run them
  • whatsnew entry

@simonjayhawkins simonjayhawkins added the Typing type annotations, mypy/pyright type checking label Mar 17, 2021
Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

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

Thanks @jbrockmendel generally lgtm.

def _test_parse_iso8601(ts: str) -> Timestamp: ...


def format_array_from_datetime(
Copy link
Member

Choose a reason for hiding this comment

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

OT: ravel_compat decorator is not typed to preserve function signatures. so the types added here wouldn't be propagated when consistency checking

Copy link
Member Author

Choose a reason for hiding this comment

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

will add this to my list


def format_array_from_datetime(
values: np.ndarray, # np.ndarray[np.int64]
tz: Optional[tzinfo] = ...,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
tz: Optional[tzinfo] = ...,
tz: tzinfo | None = ...,


import numpy as np

from pandas import Timestamp
Copy link
Member

Choose a reason for hiding this comment

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

OT: we will need to add checks to make sure that these imports don't resolve to Any

Copy link
Member Author

Choose a reason for hiding this comment

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

yep. this ended up getting remove when i took out _test_parse_iso8601

result = result.reshape(data.shape, order=order)
# error: No overload variant of "reshape" of "_ArrayOrScalarCommon"
# matches argument types "Tuple[int, ...]", "str"
result = result.reshape(data.shape, order=order) # type: ignore[call-overload]
Copy link
Member

Choose a reason for hiding this comment

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

added ignore not needed. order is inferred as str by mypy. can override the mypy inference with a variable type annotation.

i.e. order: Literal["F", "C"] = "F" if flags.f_contiguous else "C"

Copy link
Member Author

Choose a reason for hiding this comment

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

wont using Literal here cause problems? IIUC the __future__ import makes Literal OK i function annotations, but not variable annotations

Copy link
Member

Choose a reason for hiding this comment

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

I think variable annotations (inside functions) aren't parsed by the Python interpreter even without the __future__ import, but will need to confirm this.

Copy link
Member

@simonjayhawkins simonjayhawkins Mar 17, 2021

Choose a reason for hiding this comment

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

yep. This works.

def func():
    a: Unknown
    print("Success!")

func()

and this fails

a: Unknown
print("Success!")

with NameError: name 'Unknown' is not defined

Copy link
Member Author

Choose a reason for hiding this comment

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

good call, updated

@simonjayhawkins simonjayhawkins added this to the 1.3 milestone Mar 17, 2021
@simonjayhawkins simonjayhawkins merged commit d867f16 into pandas-dev:master Mar 18, 2021
@simonjayhawkins
Copy link
Member

Thanks @jbrockmendel

@jbrockmendel jbrockmendel deleted the typ-tslibs-2 branch March 18, 2021 14:12
JulianWgs pushed a commit to JulianWgs/pandas that referenced this pull request Jul 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants