-
-
Notifications
You must be signed in to change notification settings - Fork 238
FIX: env analysis dependency imports #403
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
FIX: env analysis dependency imports #403
Conversation
|
This bug was reported in our discord server! |
phmbressan
left a comment
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.
Really good implementation. Concise, organized and easily replicable/scalable.
Still a partial review only since I will test it further later, but things are awesome so far.
Update rocketpy/tools.py Co-authored-by: Pedro Henrique Marinho Bressan <87212571+phmbressan@users.noreply.github.com>
…com/rocketpy-team/rocketpy into fix/env-analysis-dependency-imports
|
@phmbressan I was wondering if there's any easy way to test the Basically the bug passed through our git actions because we always set it to download the package with [all] option. Idk if this is a matter of solving it with better github actions commands or new creating unit test with pytest. But I think it may be important for us in the future |
You can use in Colab: I tested it here and it seems some warnings are raised when even when the packages are installed: |
Nice catch @MateusStano ! I solved the issue within the last commit. The code was trying to import "windrose>=1.6.8" instead of "windrose", of course it should not work properly. I think it is running correctly now. |
- no longer assigns the issue to its author (only for PRs) - deleted the useless auto-assign-projects file - runs the pytest without the [all] option
|
This PR is ready for a re-review now. I hope the code will be working on your machines as well. |
rocketpy/EnvironmentAnalysis.py
Outdated
| operators = [">=", "<=", "==", ">", "<"] | ||
| for requirement in env_analysis_require: | ||
| try: | ||
| module = import_optional_dependency(requirement) | ||
| except ImportError: | ||
| pckg_name = requirement | ||
| for op in operators: | ||
| pckg_name = pckg_name.split(op)[0] | ||
| is_present = importlib.util.find_spec(pckg_name) | ||
| if is_present is None: | ||
| print( | ||
| f"{requirement:20} is not installed. Some methods may not work." | ||
| f"{pckg_name:20} is not installed. Some methods may not work." | ||
| + " You can install it by running 'pip install rocketpy[env_analysis]'" | ||
| ) |
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.
Some questions:
- Have you considered, instead of performing many loops to split the package name, using the
splitfunction of the packagerenative to Python? For example:
import re
pckg_name = re.split("[<>=!]{1,2}", pckg_name)[0]- Your operator list lacks the
!=. You could add this to the list or use suggestion I made above which eliminates the need for the operators list. - This seems to be a piece of code that might be needed in the future in many instances, I believe that it would be better to encapsulate this behavior into its own separate function in tools. For instance:
import re
def check_extra_requirements(requirements):
for requirement in requirements:
pkg_name = re.split("[<>=!]{1,2}", requirement)[0]
if importlib.util.find_spec(pkg_name) is None:
... # print errorThere 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.
Thanks for solid review.
- I didn't know that. See in the last commit how i implemented your suggestion
- gotcha!
- Alright, I created a new ``tools.check_requirement_version()` function, let's see if you like it. Still prefer to keep other function in the env analysis class so we can do dedicated exception management
rocketpy/EnvironmentAnalysis.py
Outdated
| env_analysis_require = [ | ||
| "timezonefinder", | ||
| "windrose>=1.6.8", | ||
| "IPython>=8.8.0", | ||
| "ipywidgets>=7.6.3", | ||
| "jsonpickle", | ||
| ] |
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.
If the package name is splitted and only the first part of the name matters for checking if it is present, why put the version numbers in this list?
I might have overlooked something, but it seems to me that
env_analysis_require = [
"timezonefinder",
"windrose",
"IPython",
"ipywidgets",
"jsonpickle",
]would have the same effect for this function without the need of the versioning. Perhaps it would be useful to keep the versioning and also check if the installed version is compliant, it is likely that the importlib has ways to check that.
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.
Yes I had left the numbers bc I thought in the future it would be nice to have version numbering tests.
I just "hadn't realized by my own" that this future were so close, so I implemented version checks in my last commit.
This feature can be used in other classes in the future.
Hope you like it now!
Hey @MateusStano , thks for noticing that. I was wondering if there's any automatic test that we could implement to check if the simulation is running in colab environment. If weren't by you, I would never know the code isn't working there. Anyway, I have simply dropped the version requirement of ipython. I believe there's no reason for us to demand specific version of this package. Ready for a re-review then. |
|
Ready for re-review @phmbressan |
MAINT: update github workflows files
|
I just improved the version comparison of python packages. It seems that using eval() may not work in 100% of the cases, besides being a security risk. https://stackoverflow.com/questions/11887762/how-do-i-compare-version-numbers-in-python |
|
While testing the latest changes, I ran into this import error: It seems weird that the metadata is not in the standard |
phmbressan
left a comment
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.
As I mentioned I my partial review, I really enjoyed this approach to organize optional dependencies imports. Well done!
The main change this PR introduces while adding new packages is to ensure the package is also added to the optional dependency dictionary of the corresponding class exactly as it is in the setup.py.
Perhaps, in the future, it may be interesting to verify these dependencies automatically with importlib.metadata.requires('rocketpy').
|
Thank you all for solid reviewing this PR. I hereby confirm that the code is running locally an in Goggle Collab. Let's move forward. |




Pull request type
Please check the type of change your PR introduces:
Pull request checklist
Please check if your PR fulfills the following requirements, depending on the type of PR:
Code base additions (for bug fixes / features):
black rocketpy) has passed locally and any fixes were madepytest --runslow) have passed locallyWhat is the current behavior?
You cannot run "import rocketpy" without getting an error
What is the new behavior?
Inspired in pandas management of optional dependency, I introduced a short function inside tools.py that will deal with the lazy imports of Environment Analysis class. I hope you all like it!
Please notice that all the optional decencies related to Environment Analysis are actually not crucial to run the class.
Plus: I created the
requirements-optional.txtto avoid confusions.Does this introduce a breaking change?
Other information