Skip to content
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

Open
wants to merge 27 commits into
base: master
Choose a base branch
from

Conversation

Felice644
Copy link
Collaborator

-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

…tree

-create the function that searches for reqirements from requiremets.txt
-create the function that searches for standard libraries from the python core
@Felice644 Felice644 requested a review from louieQ May 9, 2022 18:25
Comment on lines 50 to 52
# non posso accedere ai set creati nella classe Repository,
# come dovrei procedere?
# self.missing_requiremet = self.imported_package.difference(Repository.)
Copy link
Collaborator

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.

@@ -19,7 +19,9 @@ class Notebook(RichRenderable):
"""

def __init__(self, path: Path):
self.imported_package = None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.imported_package = None
self.imported_packages = None

self.path: Path = path
self.missing_requiremet: set
Copy link
Collaborator

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.

Comment on lines 107 to 112
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 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.
  2. 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)

Comment on lines 75 to 78
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"):
Copy link
Collaborator

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.

Comment on lines 81 to 87
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)
Copy link
Collaborator

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)

@Felice644 Felice644 requested a review from louieQ May 11, 2022 18:31
continue
elif f.endswith("Pipfile"):
continue
path = os.path.dirname(os.path.abspath(__file__))
Copy link
Collaborator

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.

Comment on lines 79 to 82
# 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"
Copy link
Collaborator

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.

Copy link
Collaborator Author

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.

Comment on lines 88 to 89
item.strip()
dependencies.add(tuple(item.split("==")))
Copy link
Collaborator

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.

Copy link
Collaborator Author

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.

coredependecies.add(f)
break
modules = sys.modules
pattern: Pattern[str] = re.compile(r".*\\\\Python\\\\Python37\\\\lib\\\\.*.py")
Copy link
Collaborator

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.

Copy link
Collaborator Author

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.

Comment on lines 101 to 106
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)
Copy link
Collaborator

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.

if f.endswith(".py"):
coredependecies.add(f)
break
modules = sys.modules
Copy link
Collaborator

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)

@Felice644 Felice644 requested a review from louieQ May 14, 2022 15:03
@louieQ louieQ added the linting rule A new check performed by pynblint label May 22, 2022
@codecov-commenter
Copy link

codecov-commenter commented May 31, 2022

Codecov Report

Merging #89 (f24380f) into master (8c2b091) will increase coverage by 5.18%.
The diff coverage is 81.32%.

@@            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     
Flag Coverage Δ
pytest 62.03% <81.32%> (+5.18%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
pynblint/nb_linting.py 64.65% <11.53%> (-15.35%) ⬇️
pynblint/exceptions.py 50.00% <50.00%> (+50.00%) ⬆️
pynblint/core_models.py 71.59% <79.16%> (+3.94%) ⬆️
tests/unit/test_core_models.py 99.22% <100.00%> (+5.89%) ⬆️

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linting rule A new check performed by pynblint
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants