Skip to content

TYP: parsers.pyi #40508

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 12 commits into from
Apr 14, 2021
Merged

TYP: parsers.pyi #40508

merged 12 commits into from
Apr 14, 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 19, 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 only partly reviewed. comments so far.

window_size: int, # int64_t
min_periods: object,
center: object,
closed: object,
Copy link
Member

Choose a reason for hiding this comment

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

str

Comment on lines 6 to 7
min_periods: object,
center: object,
Copy link
Member

Choose a reason for hiding this comment

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

perhaps leave untyped for now unless these can accept any type. (although I accept that these are unused so could in theory accept anything without raising TypeError)


from https://github.com/python/typeshed/blob/master/CONTRIBUTING.md#conventions

When adding type hints, avoid using the Any type when possible. Reserve the use of Any for when:

the correct type cannot be expressed in the current type system; and
to avoid Union returns (see above).
Note that Any is not the correct type to use if you want to indicate that some function can accept literally anything: in those cases use object instead.


so object is the correct type, unless we want to make these consistent with get_window_bounds signature


def close(self) -> None: ...

def read(self, rows: int | None = None) -> dict[int, ArrayLike]: ...
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
def read(self, rows: int | None = None) -> dict[int, ArrayLike]: ...
def read(self, rows: int | None = ...) -> dict[int, ArrayLike]: ...

comment=...,
decimal: bytes | str = ..., # single-character only
thousands: bytes | str | None = ..., # single-character only
dtype: Dtype | dict[Any, Dtype] = ...,
Copy link
Member

Choose a reason for hiding this comment

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

we want to avoid using Any, maybe could use Hashable. (which is effectively Any for a dictionary key)

this fails with error: Dict entry 0 has incompatible type "List[<nothing>]": "int"; expected "Hashable": "int" [dict-item]

from __future__ import annotations
from typing import Hashable


def func(a: dict[Hashable, int]):
    pass


func({list(): 3})

but this passes mypy checks

from __future__ import annotations
from typing import Any


def func(a: dict[Any, int]):
    pass


func({list(): 3})

although granted you can't create a dict with an unhashable key

Traceback (most recent call last):
  File "/home/simon/t.py", line 9, in <module>
    func({list(): 3})
TypeError: unhashable type: 'list'

min_periods,
center,
closed,
self.index_array, # type: ignore[arg-type]
Copy link
Member Author

Choose a reason for hiding this comment

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

@mroeschke can we rule out getting here with self.index_array being None?

Copy link
Member

Choose a reason for hiding this comment

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

For VariableWindowIndexer, yes index_array should not be None

@jreback jreback added this to the 1.3 milestone Apr 8, 2021
@jreback
Copy link
Contributor

jreback commented Apr 8, 2021

can you merge master

@jbrockmendel
Copy link
Member Author

rebased + green

num_values,
self.window_size,
min_periods,
center, # type: ignore[arg-type]
Copy link
Member

Choose a reason for hiding this comment

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

so center could be None according to the docstring, but once passed on to cython would be interpreted as False?

Copy link
Member Author

Choose a reason for hiding this comment

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

my impression is that the annotation here is wrong, which im putting an ignore on for now

@@ -15,7 +15,7 @@ def calculate_variable_window_bounds(
int64_t window_size,
object min_periods, # unused but here to match get_window_bounds signature
bint center,
object closed,
str closed,
Copy link
Member

Choose a reason for hiding this comment

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

This could be None? does cython include implicit Optional?

Copy link
Member

Choose a reason for hiding this comment

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

maybe update docstring. since we have

    # default is 'right'
    if closed is None:
        closed = 'right'

in the code.

Copy link
Member Author

Choose a reason for hiding this comment

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

does cython include implicit Optional?

for cdef functions, implicit Optional is always in effect

@jreback jreback merged commit ce6162b into pandas-dev:master Apr 14, 2021
@jbrockmendel jbrockmendel deleted the typ-libs-6 branch April 14, 2021 14:27
yeshsurya pushed a commit to yeshsurya/pandas that referenced this pull request Apr 21, 2021
yeshsurya pushed a commit to yeshsurya/pandas that referenced this pull request May 6, 2021
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.

4 participants