Skip to content

Typing Error due to return type of astropy.units.core.def_unit #14632

Open

Description

@Jerry-Ma

Description

Hi,

It appears that forms like 10 << u.m gives error in type checking (pyright in my case):

image

The alternative form 10 << u.Unit("m") works fine.

I believe the issue is due to the computed return type of the def_unit function, which is a union of three: Unit, IrreducibleUnit, and (H0) -> Equivalency. The last item does not have the __rlshift__ operator defined.

Expected behavior

No error in type checking.

How to Reproduce

  1. pip install pyright astropy
  2. Create pyright configuration file pyrightconfig.json:
{
  "include": [
    "./test_astropy_unit_type.py"
  ],
  "useLibraryCodeForTypes": true,
}
  1. Create test python file test_astropy_unit_type.py:
import astropy.units as u

a = 10.0 << u.m
  1. Run pyright and see the following output:
...
Found 1 source file
pyright 1.1.303
/home/ma/mnt/W-Ma/toltec/random_stuff/test_astropy_unit_type.py
  /home/ma/mnt/W-Ma/toltec/random_stuff/test_astropy_unit_type.py:3:5 - error: Operator "<<" not supported for types "float" and "Unit | IrreducibleUnit | ((H0: Unknown | None = None) -> Equivalency)"
    Operator "<<" not supported for types "float" and "(H0: Unknown | None = None) -> Equivalency" (reportGeneralTypeIssues)
1 error, 0 warnings, 0 informations
Completed in 1.203sec

Versions

Linux-5.15.85-penguins-rule-x86_64-with-glibc2.35
Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0]
astropy 5.2.2
Pyright 1.1.303

Activity

  1. pllim commented on Apr 14, 2023

    @pllim
    Member

    I don't think we have full typing support yet but maybe @mhvk or @nstarman can clarify.

  2. mhvk commented on Apr 14, 2023

    @mhvk
    Contributor

    Indeed, astropy has only minimal typing support so far. That said, I'm not sure why (H0) -> Equivalency is an output type. That seems wrong. Maybe @nstarman has more of a clue...

  3. nstarman commented on Apr 15, 2023

    @nstarman
    Member

    Yes, I frequently encounter this. In the absence of actual type hints pyright tries (and fails) to infer the types. Mypy has a different approach and labels this Any. Short of adding either type support or type stubs I don't think there's a good solution.

  4. neutrinoceros commented on Jan 25, 2024

    @neutrinoceros
    Contributor

    Type hints are now actively being worked on. @nstarman do you know if this can be closed or linked to any of your existing PRs ?

  5. nstarman commented on Jan 25, 2024

    @nstarman
    Member

    Not yet. we haven't type hinted anything really in units.

  6. linked a pull request that will close this issueAdd stub files with unit definitions #17902on Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions