-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Deprecate automatic creation of empty __init__.py
files
#7386
Comments
The end state shouldn't be a global option but rather removing the legacy create init behavior altogether. This might look like the following.
|
__init__.py
files
The first step of the above is implemented in @Faqa's PR #9271, which is in the process of being merged. It introduces an incompatible change flag tracked by #10076. Steps 2 and 3 above can come after that. They can actually be merged into a single step. But that'll come down the line, since I don't anticipate flipping the first flag for a while. (We need to see how difficult the migration is, and if it requires new tooling, that'll be hard to prioritize.) We can use this bug to track the overall deprecation effort. |
A concern came up in internal review of #9271: How will a user know that a failure at execution time (from missing We already have some logic in the stub script to give users a more informative error message when a failure may be due to a different incompatible change. We can adopt a similar strategy for this change, so that when the incompatible flag is enabled, the exit code is 1, and the stderr matches The downside of this approach is that it means Bazel injects stderr spam into failing |
This effectively changes the default value of legacy_create_init to false. Technically, the attribute type has changed from boolean (default true) to tristate (default auto). Therefore, it is possible to see a semantic change in code that introspects a py_binary's or py_test's attrs dict (via native.existing_rules). We think it is unlikely this will break anyone, and in any case it is not currently possible to gate a rule's definition on an incompatible change flag. Closes #9271. Work toward #7386 and #10076. RELNOTES: [Python] Added flag --incomaptible_default_to_explicit_init_py to switch the default value of legacy_create_init to True. With this flag enabled, your py_binary and py_test targets will no longer behave as if empty __init__.py files were implicitly littered in your runfiles tree. See [#10076](#10076). PiperOrigin-RevId: 276526057
@brandjon Curious, are there some existing best practices for writing BUILD files when automatic Suppose that I have the following setup:
With automatic
With manual
This is unfortunately pretty verbose, but somewhat doable for the most part I guess.. To me, the most annoying thing seems to be that we have to explicitly add dependencies to the top-level Or maybe I'm missing something - are there existing bazel code bases that have switched automatic |
There's no recommended best practice for what dependency structure to use for |
I will take the liberty to answer, I would disagree on having the init.py files in seperate dependencies.
An init file is to indicate that is a module, for libraries this should be part of a library, I would not recommend to remove it from the srcs of the library and put it separate.
The imports "." is needed to import from this library, I think that this is unfortunate but probably relates to be able to import always from the root of the workspace. If it would be a big repository with a lot of different languages mix I don't think that scales always starting your import from the workspace. I would love to extend the example with more complex situations, I am also preparing one example to see how someone could integrate pylint into bazel. |
Likely a naive idea: Is it possible to create a merging For example, with
This magical new |
@brandjon I just realized that we missed the oportunity to do the default flip on Bazel 4. What is the status? Are there any blockers to switch the default? |
In case a __init__.py file is not present, Bazel will by default automatically create one. This can have several issues and that is why we deactivate the automatic creation. For more information see: bazelbuild/bazel#7386
Could this be added to the changes for Bazel 5? |
This is not currently being worked on. I don't think we have plans to prioritize Python rules work within Bazel, though we are looking into migrating them to Starlark (as with many other native rule sets). It's conceivable that migration might require removing edge cases and strange behaviors like this one. |
+1 for resolving this. We had a major issue with this and after a long search found a hack |
Signed-off-by: Shane Loretz <[email protected]>
Use rmw_isolation Use legacy_create_init to workaround bazelbuild/bazel#7386 Add TODO about ODR violations Use C++17
I've posted a related issue, #17691 - if explicit |
I noticed If my project flips this flag (which is very convenient for us), are we pre-adopting the future, or heading down a blind alley? |
Yeah, the flag will be flipped -- eventually. It a source of confusing behavior and bugs. I'm not sure when it'll be flipped, but it'll be after the rules_python Starlark implementation is activated (which is waiting for Bazel 7 to be released). |
@rickeylev By now the rules_python Starlark implementation exists (thanks a lot for your efforts!) and Bazel 7 is half a year old. Do you believe we can flip this flag now? |
Description of the problem / feature request:
Bazel is creating init.py files automatically. This is an issue in several situations (see bazelbuild/rules_python#55).
This behavior can be suppressed using:
https://docs.bazel.build/versions/master/be/python.html#py_test.legacy_create_init
But in case that you have a large code base with a lot of python rules this is not a good solution. It would mean that this flag needs to be set in every single rule in case that you have a folder in the root of the workspace with the same name as a python package.
When this happens, it is not so easy to figure out what is going on. For modern Python developers init.py files are like source, these are not generated files. Thus, is not intuitive that the error could be because some init files are automatically generated.
Feature requests: what underlying problem are you trying to solve with this feature?
In a first step: There should be an option to decide the behavior of legacy_create_init globally.
In a second step: The default option should be not to generate init.py files by default.
Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.
Same like bazelbuild/rules_python#55 or any setup that at the root of the workspace you have a folder called like a builtin python module (time, logging, platform, queue, etc.)
What operating system are you running Bazel on?
Ubuntu 18.04
What's the output of
bazel info release
?release 0.22.0
The text was updated successfully, but these errors were encountered: