Skip to content

small typing fixes #39348

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 1 commit into from
Jan 30, 2021
Merged

small typing fixes #39348

merged 1 commit into from
Jan 30, 2021

Conversation

Dr-Irv
Copy link
Contributor

@Dr-Irv Dr-Irv commented Jan 23, 2021

Some very small fixes to typing declarations to fix some errors found by pyright

Should not affect running code at all. mypy tests passed locally.

@@ -91,7 +91,7 @@
Suffixes = Tuple[str, str]
Ordered = Optional[bool]
JSONSerializable = Optional[Union[PythonScalar, List, Dict]]
Axes = Collection
Copy link
Member

Choose a reason for hiding this comment

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

do we actually use this? grepping for ": Axes" im mostly (only?) seeing matplotlib types

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's in the type signature for DataFrame.__init__

Copy link
Contributor

Choose a reason for hiding this comment

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

we don't want to generically type like this

@Dr-Irv
Copy link
Contributor Author

Dr-Irv commented Jan 23, 2021

@jbrockmendel Only failure was in some IO testing. Do you want to merge in anyway?

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

we don't want to generically type

@Dr-Irv
Copy link
Contributor Author

Dr-Irv commented Jan 23, 2021

we don't want to generically type

@jreback Can you explain. The changes I made were to change

Axes = Collection to Axes = Collection[Any]

and in pandas/core/dtypes/base.py change the return type of the method type from Type to Type[Any]

So aren't my changes not generically typing by specifying that you can use any type? pyright is requiring the stuff in square brackets (as best as I can tell)

@jreback
Copy link
Contributor

jreback commented Jan 23, 2021

the Any for the collections should be more specific on what actually is allowed

@Dr-Irv
Copy link
Contributor Author

Dr-Irv commented Jan 24, 2021

the Any for the collections should be more specific on what actually is allowed

@jreback That's non-trivial

In pandas/_typing.py we declare Axes as a type. It is only used in the declaration of DataFrame.__init__() as follows (from pandas/core/frame.py):

    def __init__(
        self,
        data=None,
        index: Optional[Axes] = None,
        columns: Optional[Axes] = None,
        dtype: Optional[Dtype] = None,
        copy: bool = False,
    ):

So since an index or columns can contain any kind of object, I think Any is appropriate here. Unless we really want to say that only certain types can be used to define an index or columns in a constructor.

Furthermore, the correction in this PR is to change Collection to Collection[Any] which essentially are the same, but the latter is more "proper" python.

We could also change DataFrame.__init__() to use Optional[Union[ArrayLike, Index]] instead, and I can test that change and delete the Axes declaration.

Let me know.

@jreback jreback added the Typing type annotations, mypy/pyright type checking label Jan 24, 2021
@Dr-Irv
Copy link
Contributor Author

Dr-Irv commented Jan 30, 2021

@jreback pinging you on #39348 (comment)

@jreback jreback added this to the 1.3 milestone Jan 30, 2021
@jreback jreback merged commit 2075d20 into pandas-dev:master Jan 30, 2021
@jreback
Copy link
Contributor

jreback commented Jan 30, 2021

thanks @Dr-Irv generally we don't want to use Any if at all possible, even generically, but ok

@simonjayhawkins
Copy link
Member

pyright is requiring the stuff in square brackets (as best as I can tell)

FYI we have an issue related to this. #30539

mypy has strictness flags that you can enable at milestones throughout the typing journey. Requiring type parameters is often requested during review. Adding Any just to satisfy the type checker shouldn't be necessary, as we in general prefer to leave untyped for now to indicate that we have not yet established what types are permitted.

Just like Optional, Any is a confusingly named alias. Any means that the type checker will accept any type without reporting errors and is often used in heavily dynamic code that is difficult to statically check. If indeed, the code accepts any type, the type should be object not Any. Mypy will then report errors if we try to do anything with that type that is not available on all objects.

for instance, if we do a len(x), Any would not report any errors, but using object would give us error: Argument 1 to "len" has incompatible type "object"; expected "Sized"

with mypy we can turn these strictness flags on/off globally on for individual modules

so can we please, not add Any just for pyright at this stage and maybe discuss perhaps locking down the files that pass disallow_any_generic and tackling the existing annotations missing type parameters in some other way instead.

@Dr-Irv
Copy link
Contributor Author

Dr-Irv commented Mar 16, 2021

pyright is requiring the stuff in square brackets (as best as I can tell)

FYI we have an issue related to this. #30539

mypy has strictness flags that you can enable at milestones throughout the typing journey. Requiring type parameters is often requested during review. Adding Any just to satisfy the type checker shouldn't be necessary, as we in general prefer to leave untyped for now to indicate that we have not yet established what types are permitted.

Just like Optional, Any is a confusingly named alias. Any means that the type checker will accept any type without reporting errors and is often used in heavily dynamic code that is difficult to statically check. If indeed, the code accepts any type, the type should be object not Any. Mypy will then report errors if we try to do anything with that type that is not available on all objects.

for instance, if we do a len(x), Any would not report any errors, but using object would give us error: Argument 1 to "len" has incompatible type "object"; expected "Sized"

with mypy we can turn these strictness flags on/off globally on for individual modules

so can we please, not add Any just for pyright at this stage and maybe discuss perhaps locking down the files that pass disallow_any_generic and tackling the existing annotations missing type parameters in some other way instead.

@simonjayhawkins That's fair. We're all learning the best way of doing this as we move forward. It might be worth having a page in the developer's documentation section about the "rules" (such as avoiding Any) we want to use with respect to typing the pandas code. You seem to have the best handle on that at the moment

@Dr-Irv Dr-Irv deleted the fixtypes branch February 13, 2023 20:50
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