astropy icon indicating copy to clipboard operation
astropy copied to clipboard

Turn on TCH strictness

Open nstarman opened this issue 1 year ago • 0 comments

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), TCH002 (typing-only-third-party-import) and TCH003 (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, 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.

nstarman avatar Jul 01 '24 21:07 nstarman