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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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

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.

@Avasam Avasam force-pushed the add-annotations-guidelines branch from 3faa7b0 to 59de2cf Compare July 1, 2024 16:15
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.

None yet

2 participants