Skip to content
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

Add Type annotations developer guideline #4397

Merged
merged 1 commit into from
Aug 8, 2024

Conversation

Avasam
Copy link
Contributor

@Avasam Avasam commented May 24, 2024

Summary of changes

Add Type annotations developer guideline
CC @abravalheri feel free to wordsmith. I figured these things are probably better documented with sources than left in PR discussions.

Pull Request Checklist

Comment on lines 139 to 142
Setuptools makes use of inferred return annotations to reduce verbosity,
especially for complex return types. Explicit return types can be added for
functions whose inferred return type contains ``Any``, or that are not checked by
mypy (ie.: contains no parameter *and* no return type, see
Copy link
Contributor

@abravalheri abravalheri May 30, 2024

Choose a reason for hiding this comment

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

I have a curiosity question:

In which instances mypy can correctly infer return types without degenerating to Any?
For example, I tried the following:

def fn(a: str):
    return int(a)

reveal_type(fn)
main.py:4: note: Revealed type is "def (a: builtins.str) -> Any"
Success: no issues found in 1 source file

To me the return value should be int, but mypy infers a "degenerate" Any.

"Humanly obvious" None also are not correctly inferred:

def fn(a: str):
    print(a)

reveal_type(fn)
main.py:4: note: Revealed type is "def (a: builtins.str) -> Any"
Success: no issues found in 1 source file

Is there any circumstance that it can actually get it right without explicit marks?

Copy link
Contributor Author

@Avasam Avasam May 31, 2024

Choose a reason for hiding this comment

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

Oh my... I've always (wrongly) assumed that mypy at least infers the return type for functions it does type-check...

Maybe we should instead turn on some ANN2 rules for public methods so this can be checked automatically instead of having to add it as a guideline.

https://mypy.readthedocs.io/en/stable/config_file.html#confval-disallow_incomplete_defs is also an option.

At least until if/when mypy introduces more return type inference like requested in python/mypy#4409 and python/mypy#17307

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Even w/o enforcement, running ruff check pkg_resources --fix --unsafe-fixes --select=ANN2 --ignore=ANN202,ANN204 (ignoring tests) is pretty valuable.

Let's merge #4390 and #4391 first.

@Avasam Avasam marked this pull request as draft May 31, 2024 19:07
@Avasam Avasam requested a review from abravalheri June 24, 2024 17:20
@Avasam Avasam marked this pull request as ready for review June 24, 2024 17:20
@Avasam
Copy link
Contributor Author

Avasam commented Jun 24, 2024

@abravalheri Updated and added a few more details !

@Avasam Avasam force-pushed the add-annotations-guidelines branch from ff82066 to e9f787a Compare June 24, 2024 17:23
----------------

Most standards and best practices are enforced by
[Ruff](https://docs.astral.sh/ruff/rules/)'s ``ANN2``, ``FA``, ``PYI``, ``UP``
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ANN2 added in #4409

Copy link
Contributor

Choose a reason for hiding this comment

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

I did not find the FA rules, is that PyFlake (F)?

Avoid importing private type-checking-only symbols. These are often
[typeshed](https://github.com/python/typeshed) internal details and are not
guaranteed to be stable.
Importing from ``_typeshed`` or ``typing_extensions`` is fine, but if you find
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 annoying, I can't find how to create a newline w/o creating a new paragraph in RST.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah \n is threated in RST as a space and \n\n creates a new paragraph, no other option.

@Avasam Avasam force-pushed the add-annotations-guidelines branch from 3faa7b0 to 59de2cf Compare July 1, 2024 16:15
----------------

Most standards and best practices are enforced by
[Ruff](https://docs.astral.sh/ruff/rules/)'s ``ANN2``, ``FA``, ``PYI``, ``UP``
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is markdown syntax for link, but setuptools uses RST.
There is a couple of other occurrences below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not used to RST, but I think I got it right

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you.

Reminder for my future self, probably adding something like https://github.com/sloria/sphinx-issues is useful.

@Avasam Avasam force-pushed the add-annotations-guidelines branch from 59de2cf to adc33bb Compare August 8, 2024 17:15
@Avasam Avasam force-pushed the add-annotations-guidelines branch from adc33bb to eb36524 Compare August 8, 2024 17:19
@abravalheri abravalheri merged commit aed69d1 into pypa:main Aug 8, 2024
18 checks passed
@abravalheri
Copy link
Contributor

Thank you very much.

@Avasam Avasam deleted the add-annotations-guidelines branch August 8, 2024 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants