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

fix: tests and version #37

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ repos:
rev: 23.12.1
hooks:
- id: black
args:
- --line-length=79
language_version: python3
- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
hooks:
- id: flake8
args:
- --max-line-length=88
language_version: python3
additional_dependencies: [flake8-print]
- repo: https://github.com/abdelrahman0w/ouro
rev: v0.1.1
hooks:
- id: ouro
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ ouro [-h] [-v] [--verbose] [--no-categorize] [-e] [-i IGNORE [IGNORE ...]] [path

```yaml
- repo: https://github.com/abdelrahman0w/ouro
rev: v0.1.0
rev: v0.1.1
hooks:
- id: ouro
```
Expand Down
2 changes: 1 addition & 1 deletion ouro/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__author__ = """Abdelrahman Abdelkhalek"""
__email__ = "abdelrahman.abdelkhalek@thndr.app"
__version__ = "0.1.0"
__version__ = "0.1.1"
20 changes: 5 additions & 15 deletions ouro/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,12 @@ def main():
checker = Checker(
path=args.path, ignore=args.ignore, categorize=(not args.no_categorize)
)
cycles = checker.cycles

if cycles:
if cycles := checker.cycles:
retv = 1
possible_origins = checker.get_possible_origins(cycles)

logger.fail("FOUND CIRCULAR IMPORT(S)!", highlight=True)
logger.warn(
"PROBABLY ONE OF THE FOLLOWING IS THE ORIGIN", highlight=True
)
logger.warn("PROBABLY ONE OF THE FOLLOWING IS THE ORIGIN", highlight=True)

for possible_origin in possible_origins:
logger.warn(f" ==> {possible_origin}")
Expand All @@ -100,22 +96,16 @@ def main():
export_path = os.path.join(os.getcwd(), "ouro-report.json")

with open("ouro-report.json", "w") as f:
logger.info(
f"EXPORTING REPORT TO: {export_path}", highlight=True
)
logger.info(f"EXPORTING REPORT TO: {export_path}", highlight=True)
json.dump(cycles, f, indent=4)

logger.success(
f"REPORT EXPORTED TO: {export_path}", highlight=True
)
logger.success(f"REPORT EXPORTED TO: {export_path}", highlight=True)
else:
retv = 0

logger.success("WHOA! NO CIRCULAR IMPORT(S) FOUND!", highlight=True)

logger.info(
f"ELAPSED TIME: {time() - start_time:.2f} seconds", highlight=True
)
logger.info(f"ELAPSED TIME: {time() - start_time:.2f} seconds", highlight=True)

return retv

Expand Down
27 changes: 6 additions & 21 deletions ouro/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def _handle_node_cycle_only(
if not self._cycles.get(node.name):
self._cycles[node.name] = []

in_def = any(
def_begin <= lineno <= def_end for def_begin, def_end in node.defs
)
in_def = any(def_begin <= lineno <= def_end for def_begin, def_end in node.defs)
path_from_import_to_file = [node.name for node in path]

self._cycles[node.name].append(
Expand Down Expand Up @@ -65,9 +63,7 @@ def _handle_node_cycle_categorize(
"direct_import_in_def": [],
}

in_def = any(
def_begin <= lineno <= def_end for def_begin, def_end in node.defs
)
in_def = any(def_begin <= lineno <= def_end for def_begin, def_end in node.defs)
path_from_import_to_file = [node.name for node in path]
categories_map = {
"critical": (is_from and not in_def),
Expand All @@ -93,9 +89,7 @@ def _check_node(
self, node: "Node", node_imports: List[Tuple["Node", bool, int]]
) -> None:
for node_import, is_from, lineno in node_imports:
is_cyclic, path = self._imports_graph.is_reachable(
node_import, node
)
is_cyclic, path = self._imports_graph.is_reachable(node_import, node)

if not is_cyclic:
continue
Expand All @@ -105,9 +99,7 @@ def _check_node(
node, node_import, lineno, path, is_from
)
else:
self._handle_node_cycle_only(
node, node_import, lineno, path, is_from
)
self._handle_node_cycle_only(node, node_import, lineno, path, is_from)

def _check_all(self):
for node, node_imports in self._imports_graph:
Expand All @@ -133,9 +125,7 @@ def get_possible_origins(
cycle_info for cycle in cycles.values() for cycle_info in cycle
)

paths = [
cycle_info["path_from_import_to_file"] for cycle_info in cycle_iter
]
paths = [cycle_info["path_from_import_to_file"] for cycle_info in cycle_iter]
paths = [path for path_list in paths for path in path_list]
if not paths:
return []
Expand All @@ -148,9 +138,4 @@ def get_possible_origins(
most_common_paths = sorted_paths[:num_possibilities]
most_common_paths.reverse()

possible_origins = []
for path in most_common_paths:
if path in cycles:
possible_origins.append(path)

return possible_origins
return [path for path in most_common_paths if path in cycles]
25 changes: 10 additions & 15 deletions ouro/nodes_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ def __init__(
name: str,
) -> None:
self.name = name
self.imports: List[
Tuple["Node", bool, int]
] = [] # (node, is_from, lineno)
self.imports: List[Tuple["Node", bool, int]] = [] # (node, is_from, lineno)
self.defs: List[Tuple[int, int]] = []

def add(self, item: "Node", is_from: bool, lineno: int) -> None:
Expand All @@ -37,9 +35,7 @@ def _get_node(self, file: str) -> "Node":

return self.nodes[file]

def _get_imports(
self, content: str
) -> List[Union[ast.Import, ast.ImportFrom]]:
def _get_imports(self, content: str) -> List[Union[ast.Import, ast.ImportFrom]]:
return [
node
for node in ast.walk(ast.parse(content))
Expand Down Expand Up @@ -74,8 +70,7 @@ def _get_module_path(
).with_suffix(".py")
if not Path.exists(path):
path = Path(
Path(self._prg_path).parent
/ Path(*(node.names[0].name.split(".")))
Path(self._prg_path).parent / Path(*(node.names[0].name.split(".")))
).with_suffix(".py")

return (str(path), str(path))
Expand All @@ -88,8 +83,7 @@ def _get_module_path(
).with_suffix(".py")
if not Path.exists(path_1):
path_1 = Path(
Path(self._prg_path).parent
/ Path(*(node.module.split(".")))
Path(self._prg_path).parent / Path(*(node.module.split(".")))
).with_suffix(".py")

path_2 = Path(
Expand All @@ -112,16 +106,17 @@ def _initialize(self) -> None:
for file, content in self._prj:
node = self._get_node(file)
node.defs = [
(def_.lineno, def_.end_lineno)
if def_.end_lineno
else (def_.lineno, def_.lineno)
(
(def_.lineno, def_.end_lineno)
if def_.end_lineno
else (def_.lineno, def_.lineno)
)
for def_ in self._get_defs(content)
]

imports = self._get_imports(content)
for import_ in imports:
imported_module_paths = self._get_module_path(import_)
if imported_module_paths:
if imported_module_paths := self._get_module_path(import_):
path_1, path_2 = imported_module_paths
imported_node_1 = self._get_node(path_1)
imported_node_2 = self._get_node(path_2)
Expand Down
4 changes: 1 addition & 3 deletions ouro/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ def _valid_files(self) -> Generator:

for file in files:
file_path = os.path.join(dir, file)
if self._is_checkable(file_path) and not self._is_ignored(
file_path
):
if self._is_checkable(file_path) and not self._is_ignored(file_path):
yield file_path

@property
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.1.0
current_version = 0.1.1
commit = True
tag = True

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@
test_suite="tests",
tests_require=test_requirements,
url="https://github.com/abdelrahman0w/ouro",
version="0.1.0",
version="0.1.1",
zip_safe=False,
)
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ envlist = py38, flake8, black
[testenv:flake8]
basepython = python
deps = flake8
commands = flake8 ouro tests
commands = flake8 --max-line-length 88 ouro tests

[testenv:black]
basepython = python
deps = black
commands = black --check ouro tests
commands = black ouro tests

[testenv]
setenv =
Expand Down
Loading