diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dedfe3a..04634f3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,6 +25,9 @@ jobs: - name: Run tests run: | pytest + - name: Lint with ruff + run: | + ruff check symbex - name: Check if cog needs to be run run: | cog README.md diff --git a/README.md b/README.md index bfa30bc..71c18e5 100644 --- a/README.md +++ b/README.md @@ -173,31 +173,31 @@ cog.out( ) ]]] --> ```python -# File: symbex/lib.py Line: 106 +# File: symbex/lib.py Line: 107 def function_definition(function_node: AST) # File: symbex/lib.py Line: 13 def find_symbol_nodes(code: str, filename: str, symbols: Iterable[str]) -> List[Tuple[(AST, Optional[str])]] -# File: symbex/lib.py Line: 174 +# File: symbex/lib.py Line: 175 def class_definition(class_def) -# File: symbex/lib.py Line: 208 +# File: symbex/lib.py Line: 209 def annotation_definition(annotation: AST) -> str -# File: symbex/lib.py Line: 226 +# File: symbex/lib.py Line: 227 def read_file(path) -# File: symbex/lib.py Line: 252 +# File: symbex/lib.py Line: 253 class TypeSummary -# File: symbex/lib.py Line: 257 +# File: symbex/lib.py Line: 258 def type_summary(node: AST) -> Optional[TypeSummary] -# File: symbex/lib.py Line: 303 +# File: symbex/lib.py Line: 304 def quoted_string(s) -# File: symbex/lib.py Line: 314 +# File: symbex/lib.py Line: 315 def import_line_for_function(function_name: str, filepath: str, possible_root_dirs: List[str]) -> str # File: symbex/lib.py Line: 37 @@ -206,7 +206,7 @@ def code_for_node(code: str, node: AST, class_name: str, signatures: bool, docst # File: symbex/lib.py Line: 71 def add_docstring(definition: str, node: AST, docstrings: bool, is_method: bool) -> str -# File: symbex/lib.py Line: 81 +# File: symbex/lib.py Line: 82 def match(name: str, symbols: Iterable[str]) -> bool ``` @@ -227,7 +227,7 @@ cog.out( ) ]]] --> ```python -# File: symbex/lib.py Line: 81 +# File: symbex/lib.py Line: 82 # from symbex.lib import match def match(name: str, symbols: Iterable[str]) -> bool ``` @@ -265,7 +265,7 @@ cog.out( ) ]]] --> ```python -# File: symbex/lib.py Line: 81 +# File: symbex/lib.py Line: 82 def match(name: str, symbols: Iterable[str]) -> bool "Returns True if name matches any of the symbols, resolving wildcards" ``` diff --git a/setup.py b/setup.py index eaf4dea..f1d91a5 100644 --- a/setup.py +++ b/setup.py @@ -32,6 +32,6 @@ def get_long_description(): symbex=symbex.cli:cli """, install_requires=["click"], - extras_require={"test": ["pytest", "pytest-icdiff", "cogapp", "PyYAML"]}, + extras_require={"test": ["pytest", "pytest-icdiff", "cogapp", "PyYAML", "ruff"]}, python_requires=">=3.8", ) diff --git a/symbex/cli.py b/symbex/cli.py index dff0b36..d6a246c 100644 --- a/symbex/cli.py +++ b/symbex/cli.py @@ -232,7 +232,7 @@ def cli( module_dirs.append(mod_path.parent) else: module_files.append(mod_path) - except ModuleNotFoundError as ex: + except ModuleNotFoundError: raise click.ClickException("Module not found: {}".format(module)) directories = [*directories, *module_dirs] files = [*files, *module_files] @@ -318,10 +318,6 @@ def iterate_files(): if path.is_file(): yield path - # Filter symbols by type - def filter(node: ast.AST) -> bool: - return True - # If any --filters were supplied, handle them: if any( [ @@ -337,7 +333,7 @@ def filter(node: ast.AST) -> bool: no_init, ] ): - + # Return just nodes matching filters def filter(node: ast.AST) -> bool: # Filters must ALL match if async_ and not isinstance(node, ast.AsyncFunctionDef): @@ -371,6 +367,11 @@ def filter(node: ast.AST) -> bool: return False return True + else: + # All nodes are allowed + def filter(node: ast.AST) -> bool: + return True + pwd = pathlib.Path(".").resolve() num_matches = 0 replace_matches = [] diff --git a/symbex/lib.py b/symbex/lib.py index e9ad1a4..3f17ccd 100644 --- a/symbex/lib.py +++ b/symbex/lib.py @@ -75,7 +75,8 @@ def add_docstring(definition: str, node: AST, docstrings: bool, is_method: bool) if not docstring: return definition docstring = quoted_string(docstring) - return f"{definition}\n{textwrap.indent(docstring, ' ' if is_method else ' ')}" + wrapped = textwrap.indent(docstring, " " if is_method else " ") + return f"{definition}\n{wrapped}" def match(name: str, symbols: Iterable[str]) -> bool: diff --git a/tests/example_symbols.py b/tests/example_symbols.py index b3a15f0..45e1044 100644 --- a/tests/example_symbols.py +++ b/tests/example_symbols.py @@ -1,4 +1,4 @@ -from typing import Union, Tuple +from typing import Tuple # Function with no arguments