Skip to content

Commit

Permalink
Merge pull request #3 from pixee/dependencies-as-set
Browse files Browse the repository at this point in the history
Made dependencies a dict
  • Loading branch information
andrecsilva authored Aug 2, 2023
2 parents 73ba36a + 92c4c3f commit a0a5c95
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/dependency_manager/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def add(self, dependencies: list[str]):
continue

if dep not in self.dependencies:
self.dependencies.append(dep)
self.dependencies.update({dep: None})
self.dependencies_changed = True

def remove(self, dependencies: list[str]):
Expand All @@ -41,7 +41,7 @@ def remove(self, dependencies: list[str]):
continue

if dep in self.dependencies:
self.dependencies.remove(dep)
self.dependencies.pop(dep)
self.dependencies_changed = True

def write(self, dry_run=False):
Expand Down Expand Up @@ -73,13 +73,13 @@ def dependency_file(self) -> Union[Path, None]:
return None

@cached_property
def dependencies(self) -> list[pkg_resources.Requirement]:
def dependencies(self) -> dict[pkg_resources.Requirement, None]:
"""
Extract list of dependencies from requirements.txt file.
Same order of requirements is maintained, no alphabetical sorting is done.
"""
if not self.dependency_file:
return []
return dict()
with open(self.dependency_file, "r", encoding="utf-8") as f:
lines = f.readlines()
return list(pkg_resources.parse_requirements(lines))
return {req: None for req in pkg_resources.parse_requirements(lines)}
10 changes: 5 additions & 5 deletions tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ def get_parent_dir(self):

DependencyManager().add(["my_pkg==1"])
assert len(manager.dependencies) == 5
my_pkg = manager.dependencies[-1]
my_pkg = next(reversed(manager.dependencies))
assert str(my_pkg) == "my_pkg==1"

DependencyManager().add(["my_pkg_no_ver"])
assert len(manager.dependencies) == 6
my_pkg = manager.dependencies[-1]
my_pkg = next(reversed(manager.dependencies))
assert str(my_pkg) == "my_pkg_no_ver"

# don't add already existing dep
Expand All @@ -108,7 +108,7 @@ def get_parent_dir(self):
assert manager.dependency_file == path_with_req / "requirements.txt"
assert len(manager.dependencies) == 4

first_dep = manager.dependencies[0]
first_dep = next(iter(manager.dependencies))
manager.remove([str(first_dep)])
assert len(manager.dependencies) == 3

Expand All @@ -125,7 +125,7 @@ def get_parent_dir(self):
return path_with_req

manager = DependencyManager()
first_dep = manager.dependencies[0]
first_dep = next(iter(manager.dependencies))
manager.remove([str(first_dep)])
manager.write(dry_run=True)
with open(manager.dependency_file, "r", encoding="utf-8") as dep_file:
Expand All @@ -141,7 +141,7 @@ def get_parent_dir(self):
return path_with_req_with_cleanup

manager = DependencyManager()
first_dep = manager.dependencies[0]
first_dep = next(iter(manager.dependencies))
manager.remove([str(first_dep)])
manager.write()
with open(manager.dependency_file, "r", encoding="utf-8") as dep_file:
Expand Down

0 comments on commit a0a5c95

Please sign in to comment.