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

Support type annotations #1887

Open
bkeryan opened this issue Feb 16, 2023 · 2 comments
Open

Support type annotations #1887

bkeryan opened this issue Feb 16, 2023 · 2 comments

Comments

@bkeryan
Copy link
Contributor

bkeryan commented Feb 16, 2023

Python 3 supports optional type annotations using the typing module.

Benefits of specifying type annotations:

  • Clients can use external tools like mypy to perform static type-checking.
  • Editors such as Visual Studio Code use type annotations to provide auto-complete suggestions.

nimi-python already documents types in docstrings, so the information is already there. It would only need to be output in a different format.

@ni-jfitzger
Copy link
Collaborator

I prefer to wait until we drop Python 3.8, before adding type hints, due to the major differences in how collections are annotated between 3.8 and 3.9: https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html#useful-built-in-types

@bkeryan
Copy link
Contributor Author

bkeryan commented Oct 2, 2023

The collection types are really not a big deal. They don't even require the typing_extensions package.

For now, use from typing import List and write List[T]. When you drop Python 3.8, then use pyupgrade to rewrite any hand-written type hints: https://github.com/asottile/pyupgrade#pep-585-typing-rewrites

Using other new typing features in old Python versions is also possible, but it requires a little more work:

if typing.TYPE_CHECKING:
    if sys.version_info >= (3, 11):
        from typing import Self
    else:
        from typing_extensions import Self

class Session:
    def __enter__(self) -> Self: ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants