Skip to content

Implement Real and Complex type aliases #16753

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 1 commit into from
Jul 25, 2024
Merged

Conversation

eerovaher
Copy link
Member

Description

The Python standard library numbers module defines abstract base classes for different types of numbers, but those classes are not suitable for type checking (at least not with mypy or Pyright), so astropy should define these types itself.

Additionally, Sphinx has had trouble figuring out how to link to numbers.Real even though it is part of the standard library. If we define Real ourselves then Sphinx should be able to link to that definition without problems, which would be helpful for anyone reading our documentation. However, our public API does not refer to Real since 64eccee, so this benefit is not immediately obvious, but it might become relevant when more of astropy gets annotated.

One more thing worth thinking about is whether we should define Bool: TypeAlias = bool | np.bool_, but maybe that deserves a separate pull request.

  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

@eerovaher eerovaher added no-changelog-entry-needed typing related to type annotations labels Jul 22, 2024
@eerovaher eerovaher added this to the v7.0.0 milestone Jul 22, 2024
@eerovaher eerovaher requested review from mhvk and nstarman July 22, 2024 18:07
Copy link
Contributor

Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

import numpy as np

# The classes from the standard library `numbers` module are not suitable for
# type checking (https://github.com/python/mypy/issues/3186).
Copy link
Member

@pllim pllim Jul 23, 2024

Choose a reason for hiding this comment

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

Just so I can come back in the future and look at the issue status without clicking:

Ah, looks like python/mypy#3186 (comment) is more helpful.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, that link was helpful. Maybe add

# TODO: remove these when a standard way to deal with this has developed

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

This feels like something astropy shouldn't have to solve... surely, every other numerical package has the same problems! That said, if in the meantime this helps, and @nstarman likes this (and has no other ideas), that should be good enough (@pllim's link was helpful to see that this is indeed a wider problem where making up your own stuff is apparently encouraged...).

My inline comments are just about making clear that we hope to eventually remove things when a good upstream solution has been found.

import numpy as np

# The classes from the standard library `numbers` module are not suitable for
# type checking (https://github.com/python/mypy/issues/3186).
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, that link was helpful. Maybe add

# TODO: remove these when a standard way to deal with this has developed

# Licensed under a 3-clause BSD style license - see LICENSE.rst

"""
Experimental typing support for ``astropy``, subject to change
Copy link
Contributor

Choose a reason for hiding this comment

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

To further clarify, maybe

Aliases and utilities to help support typing in ``astropy``. Note that these
are experimental and may be stop-gap measures pending better official
typing support for numerical packages. Like most of ``astropy.utils``,
they are for internal use only and are subject to change without notice.

@mhvk
Copy link
Contributor

mhvk commented Jul 23, 2024

One more query: would it make sense to have this in astropy.units.typing for now? I see the case for possible broader use, but on the other hand, most of the rest of astropy takes either arrays or quantities.

Copy link
Member

@nstarman nstarman left a comment

Choose a reason for hiding this comment

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

LGTM! This will nicely simplify some type hints.

I agree that @mhvk's suggestion of astropy.units.typing is worthwhile to consider, but I'm happy with either placement

Also, should these be linked to / discussed in the glossary?

@eerovaher
Copy link
Member Author

I am starting to agree that these definitions should be in units instead of utils, unless anyone can think of anything outside units where they might be useful.

These types are currently only used in the private API, so there's no need to document them thoroughly. We can consider describing them in the glossary when we start using them in the public API.

@mhvk
Copy link
Contributor

mhvk commented Jul 24, 2024

OK, maybe the solution is to keep them in units/typing for now - we can have a more general utils/typing when it becomes needed.

The Python standard library `numbers` module defines abstract base
classes for different types of numbers, but those classes are not
suitable for type checking (python/mypy#3186),
so `astropy` should define these types itself.
@pllim pllim merged commit 8ea66df into astropy:main Jul 25, 2024
28 checks passed
@pllim pllim removed the utils label Jul 25, 2024
@eerovaher eerovaher deleted the numeric-types branch July 25, 2024 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants