astropy / astropy/astropy

Turn on TCH strictness

Open
#16,647 2 comments 1 reaction 0 assignees View on GitHub
dev-automation Feature Request
Dominant language
Python
Stars
5.3k
Forks
2.2k
Avg merge
1d 19h
Merged PRs (30d)
75

Description

> I would prefer to import as much as possible in `if TYPE_CHECKING:` blocks because that helps prevent import loops, avoids cluttering the module namespace and prevents runtime imports from modules that would not otherwise have to be loaded or might not even be installed. Ruff has the [TCH001 (typing-only-first-party-import)](https://docs.astral.sh/ruff/rules/typing-only-first-party-import/), [TCH002 (typing-only-third-party-import)](https://docs.astral.sh/ruff/rules/typing-only-third-party-import/) and [TCH003 (typing-only-standard-library-import)](https://docs.astral.sh/ruff/rules/typing-only-standard-library-import/) rules to help enforce that, but by default those rules don't edit imports from the modules from which there are also runtime imports. That is because if there are runtime import anyways then typing-only imports cannot cause import loops and they do not trigger needless imports at runtime. However, they still clutter the namespace and knowing that something is only needed for type checking is informative for the reader. Luckily [Ruff does have a setting for preventing runtime typing-only imports even from modules that are imported at runtime anyways](https://docs.astral.sh/ruff/settings/#lint_flake8-type-checking_strict), but we need to turn that setting on (in a separate pull request).

_Originally posted by @eerovaher in https://github.com/astropy/astropy/pull/15920#discussion_r1648103631_

This would not make anyone's life harder, just enforce that non-required-at-runtime imports are not imported at runtime.
The perhaps blocker is `dataclasses.KW_ONLY`, which are annotations required at runtime.

Contributor guide

Open the contributing guide

Research direction

Start by locating Astropy's Ruff configuration and reviewing how the TCH001, TCH002, and TCH003 rules are configured. Enable Ruff's flake8-type-checking strict setting, then run the relevant lint checks across the repository and inspect runtime uses such as dataclasses.KW_ONLY. Done means the strict check is enabled and any resulting imports are handled without breaking required runtime behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.