astropy icon indicating copy to clipboard operation
astropy copied to clipboard

cosmo: simplifications to `__all__`

Open nstarman opened this issue 2 months ago • 6 comments

If all the other modules manage their __all__ properly then __init__.py files could use wildcard imports and implicit __all__. This is permitted by PEP 8:

There is one defensible use case for a wildcard import, which is to republish an internal interface as part of a public API

I am pointing it out here, but it also applies to other __init__.py files.

Originally posted by @eerovaher in https://github.com/astropy/astropy/pull/18884#discussion_r2524609473

nstarman avatar Nov 13 '25 23:11 nstarman

Looking at this more, I'd like to test this with a type checker. I'm confident in __all__, but IDK how importing to public API from a private __init__.py works. If it works well, that'd be very nice.

nstarman avatar Nov 14 '25 07:11 nstarman

tl;dr -- Is this just plain refactoring or it changes the user API?

pllim avatar Nov 14 '25 15:11 pllim

The title of the issue is bad. It's __init__.py files that could be simplified, there should be no changes to any __all__.

eerovaher avatar Nov 14 '25 15:11 eerovaher

No api changes.

nstarman avatar Nov 14 '25 16:11 nstarman

Stuff that needs to be fixed:

  • https://github.com/astropy/astropy/blob/main/astropy/cosmology/_src/funcs/comparison.py doesn't list cosmology_equal, but it's in https://github.com/astropy/astropy/blob/main/astropy/cosmology/_src/funcs/init.py.

nstarman avatar Nov 14 '25 21:11 nstarman

See main.py in https://play.ty.dev/313a15da-739e-4797-a10b-ae4cace54395 for how imports with a hierarchical structure might be complicated. Needs investigation! Edit: https://play.ty.dev/313a15da-739e-4797-a10b-ae4cace54395 shows this may work well, with ty at least.

nstarman avatar Nov 14 '25 22:11 nstarman