Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Adds typehints to TimeRange properties returning astropy.time.Time#8614

Open
daraghhollman wants to merge 1 commit into
sunpy:mainfrom
daraghhollman:feat/typehint-astropy-time-in-timerange
Open

Adds typehints to TimeRange properties returning astropy.time.Time#8614
daraghhollman wants to merge 1 commit into
sunpy:mainfrom
daraghhollman:feat/typehint-astropy-time-in-timerange

Conversation

@daraghhollman
Copy link
Copy Markdown
Contributor

@daraghhollman daraghhollman commented May 6, 2026

PR Description

Adds return typehints for TimeRange.start, TimeRange.end, and TimeRange.center, which all return astropy.time.Time.

Due to the way the __init__ method in TimeRange is structured, LSPs may interpret the output of the above properties as None | Time, which can show an error when accessing the attributes and methods within (however there are no issues at runtime).

e.g. for:

from sunpy.time import TimeRange

time_range = TimeRange("2020-01-01", "2021-01-01")

time_range.start.to_datetime()

error| "to_datetime" is not a known attribute of "None"
(pyright)

From my testing, TimeRange.center doesn't behave like this, however I added the hint for consistency with the start and end properties.

These changes don't affect runtime behaviour, but makes the package easier to work with in practice.

AI Assistance Disclosure

AI tools were used for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding
  • No AI tools were used

Regardless of AI use, the human contributor remains fully responsible for correctness, design choices, licensing compatibility, and long-term maintainability.

…ime` objects

Adds return typehints for `TimeRange.start`, `TimeRange.end`, and
`TimeRange.center`, which all return `astropy.time.Time`.

Because of the complex `__init__` method in `TimeRange`, some LSPs
interpret the output of the above attributes as `None | Time`, which can
lead to LSPs showing an error when accessing the attributes and methods
within.

e.g. for:

`time_range.start.to_datetime()`

'error| "to_datetime" is not a known attribute of "None"'

(The above output is from pyright)

This commit makes no runtime changes.
@daraghhollman daraghhollman changed the title Adds typehints to TimeRange attributes returning astropy.time.Time Adds typehints to TimeRange properties returning astropy.time.Time May 7, 2026
Copy link
Copy Markdown
Member

@Cadair Cadair left a comment

Choose a reason for hiding this comment

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

Hi,thanks for the PR.

Could you please remove the return type in the docstring, as we don't need that information in two places. Once you've done this please verify that the built documentation has correctly displayed the return type from the function annotation.

Thanks!!

@daraghhollman
Copy link
Copy Markdown
Contributor Author

Hi @Cadair, it seems the documentation relies on the docstring only. After removing the type from the docstring, i.e. going from:

    @property
    def start(self) -> Time:
        """
        Get the start time.

        Returns
        -------
        `astropy.time.Time`
            The start time.
        """
        return self._t1

to:

    @property
    def start(self) -> Time:
        """
        Get the start time.

        Returns
        -------
            The start time.
        """
        return self._t1

the built docs don't show any type. See screenshots from /docs/_build/html/generated/api/sunpy.time.TimeRange.html:

Before:
image

After:
image

It would seem the current framework doesn't interpret the type from hints? Or have I misinterpreted what you meant?

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