-
Notifications
You must be signed in to change notification settings - Fork 2
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
provisional function on imports #89
base: master
Are you sure you want to change the base?
Conversation
…tree -create the function that searches for reqirements from requiremets.txt -create the function that searches for standard libraries from the python core
pynblint/notebook.py
Outdated
# non posso accedere ai set creati nella classe Repository, | ||
# come dovrei procedere? | ||
# self.missing_requiremet = self.imported_package.difference(Repository.) |
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.
This check should be performed within a dedicated linting function (inside nb_linting.py
, I guess).
Here we are just defining notebook properties; the notebook property that we need to add in order to make the check feasible is the set of imported packages, as you already do at line 49.
pynblint/notebook.py
Outdated
@@ -19,7 +19,9 @@ class Notebook(RichRenderable): | |||
""" | |||
|
|||
def __init__(self, path: Path): | |||
self.imported_package = None |
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.
self.imported_package = None | |
self.imported_packages = None |
pynblint/notebook.py
Outdated
self.path: Path = path | ||
self.missing_requiremet: set |
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.
This information is not supposed to be stored here. It will be computed as the result of a linting function dedicated to checking the presence of import statements that do not map to the declared project requirements.
pynblint/repository.py
Outdated
for i in sys.path: | ||
if i.endswith("\\lib\\site-packages"): | ||
for root, dirs, files in os.walk(i): | ||
for f in files: | ||
if f.endswith(".py"): | ||
coredependecies.add(f) |
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 you can read in this Stack Overflow post,
site-packages
does not contain just Python's core packages, but it's rather "the target directory of manually built Python packages" in general. - Is a dynamic check like this really necessary? This seems like a fixed information to me (at least if we consider a single Python version at a time)
pynblint/repository.py
Outdated
for root, dirs, files in os.walk(self.path): | ||
for f in files: | ||
# check if exist a requiremets.txt file | ||
if f.endswith("requirements.txt"): |
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.
This recursive search is not necessary. requirements.txt
– as well as other files in which dependency declaration typically occurs – is expected to be placed at the root of a Python project.
pynblint/repository.py
Outdated
file_row = fi.read() | ||
# print(file_row) | ||
tmp = file_row.split("\n") | ||
for item in tmp: | ||
dependencies.add(item) | ||
# dependencies.add(file_row) | ||
# dependencies.add(file_row) |
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.
When you parse a package from requirements.txt
, make sure to strip the version constraint information (e.g., the minimum version number)
… the excessive recursion created earlier.
pynblint/repository.py
Outdated
continue | ||
elif f.endswith("Pipfile"): | ||
continue | ||
path = os.path.dirname(os.path.abspath(__file__)) |
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.
We don't need to dynamically fetch this information: the path of a repository is already present as a property in the class Repository
. Use self.path
to get it.
pynblint/repository.py
Outdated
# siccome il file di requirement si toverà nella root di | ||
# progetto ho creato un file fittizio con le stesse | ||
# informazioni per non spostare quello pre-esistente | ||
file = "kk.txt" |
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.
I don't understand this.
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.
the requirement.txt file that is present in the repository is located inside the "docs" folder and not in the project root where it should actually be. I wrote this passage in anticipation of what I believe is the future location of "requirement.txt". If, on the other hand, it should remain there where it is, I can change the route without problems.
pynblint/repository.py
Outdated
item.strip() | ||
dependencies.add(tuple(item.split("=="))) |
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.
Saving dependencies as tuples is unnecessary. You can save them as simple strings, stripping the version info (which we cannot deduce from the notebook).
So the certifi==2021.10.8
entry in requirements.txt
would be stored as certifi
in Pynblint.
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.
I have inserted the step of the split to make it easier to check with the versions of the libraries to look for. Now I fix this step.
pynblint/repository.py
Outdated
coredependecies.add(f) | ||
break | ||
modules = sys.modules | ||
pattern: Pattern[str] = re.compile(r".*\\\\Python\\\\Python37\\\\lib\\\\.*.py") |
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.
This regex is tied to your installation of Python. It would not work with a different Python version (e.g., Python 3.8) or a different virtual environment name.
At least, I would generalize the Python version.
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.
Of course, it was an oversight. I correct it immediately.
pynblint/repository.py
Outdated
for i in modules.items(): | ||
# print(" PPOSIZIONE 0:" + i[0]) | ||
# print(" PPOSIZIONE 1:" + str(i)) | ||
if pattern.match(str(i)): | ||
coredependecies.add(i[0]) | ||
print(coredependecies) |
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.
I guess these are debug print statements that will be removed.
pynblint/repository.py
Outdated
if f.endswith(".py"): | ||
coredependecies.add(f) | ||
break | ||
modules = sys.modules |
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.
Are you sure that this solution is appropriate? From this SO thread I learn that:
sys.modules
- only shows modules that have already been loaded
The solution proposed in one of the answers to the same SO question seems to be a lot cleaner to me and might be the way to go: i.e., using isort's place_module
function or getting the list of core modules like this:
from isort.stdlibs.py39 import stdlib
for name in sorted(stdlib): print(name)
- Added the test class and related tests for the functions present in the repository.
- Refactoring of code in repository
…modules/packeges_imported # Conflicts: # pynblint/notebook.py
- Minor changes in the repository class.
The parser funcion for `environment.yml` files is ready and tested.
Add further test cases for `Notebook._get_imported_packages`
Updates and fixes the functions to detect undeclared dependencies
Codecov Report
@@ Coverage Diff @@
## master #89 +/- ##
==========================================
+ Coverage 56.84% 62.03% +5.18%
==========================================
Files 18 18
Lines 913 1151 +238
==========================================
+ Hits 519 714 +195
- Misses 394 437 +43
Flags with carried forward coverage won't be shown. Click here to find out more.
📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more |
-create the function that searches for imports from the notebook tree
-create the function that searches for reqirements from requiremets.txt
-create the function that searches for standard libraries from the python core