Skip to content

TYP: returning None for __init__ #46337

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

Closed
jreback opened this issue Mar 12, 2022 · 5 comments · Fixed by #48191
Closed

TYP: returning None for __init__ #46337

jreback opened this issue Mar 12, 2022 · 5 comments · Fixed by #48191
Labels
Typing type annotations, mypy/pyright type checking

Comments

@jreback
Copy link
Contributor

jreback commented Mar 12, 2022

xref #44677 (comment)

we originally had a decision not to type annotate returning None for init

can other folks weigh in here? We have a number of PRs which do exactly this (I merged one or 2 then saw the comments)
e.g. #46283 and a few others.

cc @simonjayhawkins @twoertwein @Dr-Irv @phofl

@jreback jreback added the Typing type annotations, mypy/pyright type checking label Mar 12, 2022
@jreback jreback added this to the 1.5 milestone Mar 12, 2022
@twoertwein
Copy link
Member

I'm in favor of adding -> None (but wouldn't object for not adding it either)

From PEP 484:

(Note that the return type of init ought to be annotated with -> None. The reason for this is subtle. If init assumed a return annotation of -> None, would that mean that an argument-less, un-annotated init method should still be type-checked? Rather than leaving this ambiguous or introducing an exception to the exception, we simply say that init ought to have a return annotation; the default behavior is thus the same as for other methods.)

As far as I understand it: we should definitely add -> None if we have an __init__ that otherwise wouldn't have any type annotations.

@Dr-Irv
Copy link
Contributor

Dr-Irv commented Mar 12, 2022

Looking at numpy, it seems they have done this, and since they are way ahead on typing than we are, we should follow in the footsteps of giants and thus I favor adding -> None to the return types for __init__() throughout the code, probably as one big PR.

@simonjayhawkins
Copy link
Member

I think that now we are looking to make the pandas types public, we should probably add the None since the assumed return type was a mypy thing and users type checking their code may use other type checkers, so we should probably now follow the pep.

In the early days, before we had check_untyped_defs=True, adding the None ensured that the function body was checked for __init__ methods with no arguments to type, so there would have been some advantage to adding it selectively, but it was decided that in general it was redundant and therefore not needed.

@simonjayhawkins
Copy link
Member

but it was decided that in general it was redundant and therefore not needed.

from #27418 (comment)

pseudo-standard internally not to add

so maybe why not formalized in https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#style-guidelines.

@jreback
Copy link
Contributor Author

jreback commented Mar 16, 2022

ok i am going to merge the existing PRs.

let's close this issue with an update for the style guide. (and ideally change everything in a small number of PRs)

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 a pull request may close this issue.

5 participants