New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace import * with explicit imports #645
base: main
Are you sure you want to change the base?
Conversation
I'm not philosophically opposed to replacing the |
I have no issue with removing the notes about the autogeneration process from the code. On your point about preserving the API exactly as is, the static version of mkinit does not look at what On pip install of yaml 6.0 the result from
and the new version I get:
And this verifies that nothing has been removed from the top level API.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a couple of other minor seemingly unrelated/unnecessary changes to zap, then this looks good to go. Thanks for double-checking the equivalence (and "showing your work" 😉). With our current workflow, we don't usually merge things to a release branch until we're actually prepping a release, but with those couple changes, this one looks good to go for the next release- I'll mark it as such and we'll merge it whenever we do one (probably sometime before Python 3.11 final release, but TBD). Thanks!
@ingydotnet I had originally planned to try a first PR where I added some documentation, but I re-prioritized this first. I find it very difficult to nagivate code that use
import *
because it's not immediately clear what is coming from where, especially if there are more than one of them. However,import *
is undeniably useful when you are in the midst of writing a repo and you already know what lives where (or in other use cases where you need to mirror another module dynamically).This is the reason I wrote mkinit, which is a library that helps autogenerate code roughly equivalent to
import *
. I rank the program on this repo and it doesn't seem there is any crazy dynamic logic that would mess up mkinit's static analysis. The code in the__init__.py
is autogenerated as-is, I defined the__submodules__
attribute to restrict mkinit to only working with those modules that were alreadyimport *
-ed and I added a__dev__
note comment that shows a user the commands that were used to do the auto-generation.For the other files, I used mkinit to help, but it only works on
__init__.py
files at the moment (although, this use case does inspire me to upgrade it such that it can work it's magic to actually replaceimport *
in files rather than just generating a specified__init__.py
file). But what I did was I ranmkinit
on the entire repo and told it to generate attrs for all the submodules, and then I did a manual copy/paste to overwrite things where appropriate.Also, unless I missed some magic logic that defined a global
loader
variable inlib/yaml/__init__.py
, then the functionsadd_constructor
,add_path_resolver
, etc... have a NameError in one of their code paths. If this is real, this does serve as evidence for the value of explicitly stating your imports. It makes it a lot easier for linters like pyflake to catch these errors (or maybe I'm just missing how that global was supposed to be populated).