Skip to content

TYP: simple return types #54786

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
Aug 31, 2023
Merged

TYP: simple return types #54786

merged 12 commits into from
Aug 31, 2023

Conversation

twoertwein
Copy link
Member

@twoertwein twoertwein commented Aug 28, 2023

"simple" meaning, non-unions and non-overload return types (at least in most cases).

xref #47521

edit: sorry for changes in many files - I grep'ed for missing return types inferred by pyright and then fixed the resulting type errors.

@@ -110,7 +110,7 @@
)


IntervalSideT = Union[TimeArrayLike, np.ndarray]
IntervalSide = Union[TimeArrayLike, np.ndarray]
Copy link
Member Author

Choose a reason for hiding this comment

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

...T is often used for TypeVars

@@ -349,8 +350,8 @@ class BaseExprVisitor(ast.NodeVisitor):
preparser : callable
"""

const_type: type[Term] = Constant
term_type = Term
const_type: ClassVar[type[Constant]] = Constant
Copy link
Member Author

Choose a reason for hiding this comment

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

Should probably have one PR just focused on making ClassVars more explicit (type checkers have difficulties determining whether they are class or instance variables).

@@ -770,6 +772,7 @@ def value_counts(
mask = ids != -1
ids, val = ids[mask], val[mask]

lab: Index | np.ndarray
Copy link
Member Author

Choose a reason for hiding this comment

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

This pattern happens quite often because of mypy's early binding assumption (first declaration, even if only in one branch, determines the type). I prefer pyright's behavior:

if foo():
    a = 1 # mypy: a is of type int (in this branch, in all other branches, and after the if/else)
    # pyright: a is of type int in this branch
else: 
    a = "1" # mypy error!
    # pyright: a is of type str in this branch
# pyright: a is of type int | str

Copy link
Member

Choose a reason for hiding this comment

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

No real opinion here - mypy forces us to be more explicit for readers prior to branching which I think is a plus.

@twoertwein twoertwein marked this pull request as ready for review August 28, 2023 16:09
@twoertwein twoertwein requested a review from rhshadrach as a code owner August 28, 2023 16:09
@@ -770,6 +772,7 @@ def value_counts(
mask = ids != -1
ids, val = ids[mask], val[mask]

lab: Index | np.ndarray
Copy link
Member

Choose a reason for hiding this comment

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

No real opinion here - mypy forces us to be more explicit for readers prior to branching which I think is a plus.

@twoertwein
Copy link
Member Author

Thank you @rhshadrach for your thorough review! I think I addressed your comments as best as I can.

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

lgtm

@twoertwein twoertwein requested a review from mroeschke August 30, 2023 21:22
@mroeschke mroeschke added this to the 2.2 milestone Aug 31, 2023
@mroeschke mroeschke merged commit c74a071 into pandas-dev:main Aug 31, 2023
@mroeschke
Copy link
Member

Thanks @twoertwein

@twoertwein
Copy link
Member Author

hm, the docs timed out on main as well - did the annotation cause the docs to fail?

@mroeschke
Copy link
Member

I think that's unrelated (my running theory is that something processing the whatsnew files is not parallel safe and hanging)

mroeschke added a commit to mroeschke/pandas that referenced this pull request Sep 11, 2023
* Return None

* Return simple types

* ruff false positive

* isort+mypy

* typo, use " for cast

* SingleArrayManager.dtype can also be a numpy dtype

* comments + test assert on CI

* wider return types at the cost of one fewer mypy ignore

* DatetimeArray reaches IntervalArray._combined

* avoid some ignores

* remove assert False
@twoertwein twoertwein deleted the type branch September 19, 2023 15:24
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.

3 participants