From de1d0c89566a12043fe82bb41c73deac15c6f666 Mon Sep 17 00:00:00 2001 From: Daniel Stoops Date: Thu, 14 Mar 2024 13:04:44 +0200 Subject: [PATCH 1/4] Use poetry + update packages --- .pre-commit-config.yaml | 52 +++++++------- Pipfile | 21 ------ pyproject.toml | 79 ++++++++++++++++++++-- {src/refers => refers}/__init__.py | 0 {src/refers => refers}/cli.py | 1 + {src/refers => refers}/compromise_black.py | 22 +++--- {src/refers => refers}/definitions.py | 0 {src/refers => refers}/errors.py | 0 {src/refers => refers}/refers.py | 54 ++++++--------- {src/refers => refers}/tags.py | 31 +++++---- requirements.txt | 3 - requirements_dev.txt | 7 -- setup.cfg | 79 ---------------------- setup.py | 4 -- 14 files changed, 147 insertions(+), 206 deletions(-) delete mode 100644 Pipfile rename {src/refers => refers}/__init__.py (100%) rename {src/refers => refers}/cli.py (99%) rename {src/refers => refers}/compromise_black.py (98%) rename {src/refers => refers}/definitions.py (100%) rename {src/refers => refers}/errors.py (100%) rename {src/refers => refers}/refers.py (95%) rename {src/refers => refers}/tags.py (88%) delete mode 100644 requirements.txt delete mode 100644 requirements_dev.txt delete mode 100644 setup.cfg delete mode 100644 setup.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f3e71ca..ede0090 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,14 +2,8 @@ default_language_version: python: python3.10 repos: -- repo: https://github.com/psf/black - rev: 23.3.0 - hooks: - - id: black - args: [--safe] - - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.2.0 + rev: v4.5.0 hooks: # - id: trailing-whitespace - id: end-of-file-fixer @@ -18,31 +12,18 @@ repos: - id: debug-statements language_version: python3 -- repo: https://github.com/PyCQA/flake8 - rev: 6.0.0 +- repo: https://github.com/asottile/reorder_python_imports + rev: v3.12.0 hooks: - - id: flake8 - language_version: python3 - -# - repo: https://github.com/asottile/reorder_python_imports -# rev: v3.9.0 -# hooks: -# - id: reorder-python-imports -# args: [--py36-plus] + - id: reorder-python-imports + args: [--py36-plus] - repo: https://github.com/asottile/pyupgrade - rev: v2.32.1 + rev: v3.15.1 hooks: - id: pyupgrade args: [--py36-plus] -# - repo: https://github.com/pre-commit/mirrors-mypy -# rev: v0.960 -# hooks: -# - id: mypy -# files: ^src/ -# args: [] - - repo: local hooks: - id: pytest-check @@ -51,9 +32,24 @@ repos: language: system pass_filenames: false always_run: true - -- repo: local - hooks: + - id: black + name: black + entry: black . + language: system + pass_filenames: false + always_run: true + - id: mypy + name: mypy + entry: mypy . + language: system + pass_filenames: false + always_run: true + - id: flake8 + name: flake8 + entry: flake8 + language: system + pass_filenames: false + always_run: true - id: tox-check name: tox entry: tox diff --git a/Pipfile b/Pipfile deleted file mode 100644 index fbf5610..0000000 --- a/Pipfile +++ /dev/null @@ -1,21 +0,0 @@ -[[source]] -url = "https://pypi.org/simple" -verify_ssl = true -name = "pypi" - -[packages] -toml = "~=0.10.2" -black = ">=23.0.0" - -[dev-packages] -tox = "*" -flake8 = "*" -mypy = "*" -pre-commit = "*" -types-toml = "*" -pytest = "*" -reorder-python-imports = "*" - -[requires] -python_version = "3.10" -python_full_version = "3.10.10" diff --git a/pyproject.toml b/pyproject.toml index d61ee40..f9869c3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,9 +1,36 @@ +[tool.poetry] +name = "refers" +version = "0.0.3" +description = "Reference code to improve explainability" +authors = ["Daniel Stoops "] +readme = "README.md" +license = "MIT" + +[tool.poetry.dependencies] +python = "^3.11" +toml = "^0.10.2" +black = "^24.2.0" + +[tool.poetry.group.dev.dependencies] +tox = "^4.14.1" +flake8 = "^7.0.0" +mypy = "^1.9.0" +pre-commit = "^3.6.2" +types-toml = "^0.10.8.20240310" +pytest = "^8.1.1" +reorder-python-imports = "^3.12.0" +pytest-cov = "^4.1.0" +flake8-pyproject = "^1.2.3" + [build-system] -requires = ["setuptools>=42.0", "wheel"] -build-backend = "setuptools.build_meta" +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" + +[tool.poetry.scripts] +refers = "refers.cli:run" [tool.mypy] -mypy_path = "src" +mypy_path = "refers" check_untyped_defs = true disallow_any_generics = true ignore_missing_imports = true @@ -14,4 +41,48 @@ warn_redundant_casts = true warn_return_any = true warn_unreachable = true warn_unused_configs = true -no_implicit_reexport = true \ No newline at end of file +no_implicit_reexport = true + +[tool.pytest.ini_options] +testpaths = "tests" +addopts = [ + "--cov=refers", + "--cov-fail-under=75", + "--cov-report=term-missing", + "--cov-report=term", + "--cov-report=" +] + +[tool.tox] +legacy_tox_ini = """ +[tox] +envlist = + py{311} + lint +isolated_build = true + +[testenv] +deps = + pytest>=8.1.1 + pytest-cov>=4.1.0 +setenv = + PYTHONPATH = {toxinidir} +commands = + python -c "import sys; print(sys.version_info)" + python -m pytest --basetemp={envtmpdir} + +[testenv:lint] +deps = + flake8>=7.0.0 + mypy>=1.9.0 + black>=24.2.0 +commands = + python -m flake8 refers/ tests/ + python -m mypy refers/ tests/ + python -m black --check --diff . +""" + +[tool.flake8] +ignore = ['E203', 'E501', 'W503'] +max-line-length = 88 +count = true diff --git a/src/refers/__init__.py b/refers/__init__.py similarity index 100% rename from src/refers/__init__.py rename to refers/__init__.py diff --git a/src/refers/cli.py b/refers/cli.py similarity index 99% rename from src/refers/cli.py rename to refers/cli.py index 368d355..056a6da 100644 --- a/src/refers/cli.py +++ b/refers/cli.py @@ -1,4 +1,5 @@ import argparse + from refers.refers import format_doc diff --git a/src/refers/compromise_black.py b/refers/compromise_black.py similarity index 98% rename from src/refers/compromise_black.py rename to refers/compromise_black.py index 5a0623f..cb88083 100644 --- a/src/refers/compromise_black.py +++ b/refers/compromise_black.py @@ -1,10 +1,11 @@ -"""sorry black, you've been compromised""" from functools import partial from typing import Iterator from typing import Set from typing import TypeVar +from typing import Union from black.comments import generate_comments +from black.linegen import Line # type:ignore from black.mode import Mode from black.nodes import ASSIGNMENTS from black.nodes import is_name_token @@ -14,22 +15,16 @@ from black.nodes import syms from black.nodes import Visitor from black.nodes import WHITESPACE +from blib2to3.pgen2 import token +from blib2to3.pytree import Leaf +from blib2to3.pytree import Node + +"""sorry black, you've been compromised""" # types T = TypeVar("T") Index = int LeafID = int - - -from blib2to3.pgen2 import token - -from typing import ( - Union, -) - -from blib2to3.pytree import Node, Leaf # type: ignore -from black.linegen import normalize_prefix, Line - LN = Union[Leaf, Node] @@ -85,7 +80,8 @@ def visit_default(self, node: LN) -> Iterator[Line]: self.current_line.append(comment) yield from self.line() - normalize_prefix(node, inside_brackets=any_open_brackets) + if any_open_brackets: + node.prefix = "" # if self.mode.string_normalization and node.type == token.STRING: # node.value = normalize_string_prefix(node.value) # node.value = normalize_string_quotes(node.value) diff --git a/src/refers/definitions.py b/refers/definitions.py similarity index 100% rename from src/refers/definitions.py rename to refers/definitions.py diff --git a/src/refers/errors.py b/refers/errors.py similarity index 100% rename from src/refers/errors.py rename to refers/errors.py diff --git a/src/refers/refers.py b/refers/refers.py similarity index 95% rename from src/refers/refers.py rename to refers/refers.py index 7e7e0a2..d827399 100644 --- a/src/refers/refers.py +++ b/refers/refers.py @@ -1,47 +1,37 @@ +import re +from pathlib import Path +from typing import List +from typing import Optional from typing import ( TypeVar, ) +from typing import Union +import black +import toml from black import nodes +from black.parsing import lib2to3_parse +from blib2to3.pytree import Leaf +from blib2to3.pytree import Node + +from refers.compromise_black import LineGenerator +from refers.definitions import CODE_RE_TAG +from refers.definitions import DOC_OUT_ID +from refers.definitions import DOC_RE_TAG +from refers.definitions import LIBRARY_NAME +from refers.errors import MultipleTagsInOneLine +from refers.errors import OptionNotFoundError +from refers.errors import PyprojectNotFound +from refers.errors import TagNotFoundError +from refers.tags import Tag +from refers.tags import Tags # types T = TypeVar("T") Index = int LeafID = int -from typing import ( - List, - Optional, - TypeVar, - Union, -) - -from blib2to3.pytree import Node, Leaf # type: ignore - LN = Union[Leaf, Node] -T = TypeVar("T") -from refers.tags import Tag, Tags - - -import black -from black.parsing import lib2to3_parse - -from refers.errors import ( - MultipleTagsInOneLine, - TagNotFoundError, - PyprojectNotFound, - OptionNotFoundError, -) -import re -from pathlib import Path -from refers.definitions import ( - CODE_RE_TAG, - DOC_RE_TAG, - DOC_OUT_ID, - LIBRARY_NAME, -) -import toml -from refers.compromise_black import LineGenerator def get_files( diff --git a/src/refers/tags.py b/refers/tags.py similarity index 88% rename from src/refers/tags.py rename to refers/tags.py index 07c6dab..26701b5 100644 --- a/src/refers/tags.py +++ b/refers/tags.py @@ -2,15 +2,12 @@ import token import warnings from pathlib import Path -from typing import ( - Optional, -) +from typing import List from black.nodes import syms from blib2to3.pytree import Node # type: ignore -from refers.definitions import ( - COMMENT_SYMBOL, -) + +from refers.definitions import COMMENT_SYMBOL from refers.errors import TagAlreadyExistsError from refers.errors import TagNotFoundError from refers.errors import TagNotInClass @@ -113,13 +110,13 @@ def visit_full_line(self, *args, **kwargs) -> str: return self._full_line def visit_default(self, *args, **kwargs) -> str: - return self.file.name + " L" + str(self.line_num) + return str(self.file.name) + " L" + str(self.line_num) def visit_quotecode(self, *args, **kwargs) -> str: """return code without comments""" if self.file.suffix.lower() not in COMMENT_SYMBOL.keys(): warnings.warn(f"{self.file.suffix} not recognised. Using :quote option") - return self.full_line + return str(self.full_line) return re.sub( rf"{COMMENT_SYMBOL[self.file.suffix.lower()]}.*(\n?)", r"\1", @@ -127,19 +124,23 @@ def visit_quotecode(self, *args, **kwargs) -> str: ).strip() def visit_quote(self, *args, **kwargs) -> str: - return self.full_line + return str(self.full_line) def visit_fulllinkline(self, *args, **kwargs) -> str: - return self.file.as_posix() + "#L" + str(self.line_num) + return str(self.file.as_posix()) + "#L" + str(self.line_num) def visit_fulllink(self, *args, **kwargs) -> str: - return self.file.as_posix() + return str(self.file.as_posix()) def visit_linkline(self, parent_dir: Path, *args, **kwargs) -> str: - return self.file.relative_to(parent_dir).as_posix() + "#L" + str(self.line_num) + return ( + str(self.file.relative_to(parent_dir).as_posix()) + + "#L" + + str(self.line_num) + ) def visit_link(self, parent_dir: Path, *args, **kwargs) -> str: - return self.file.relative_to(parent_dir).as_posix() + return str(self.file.relative_to(parent_dir).as_posix()) # def visit_p(self, num_parents, *args, **kwargs) -> str: # return ( @@ -164,9 +165,9 @@ def visit_class(self, *args, **kwargs): class Tags: def __init__(self): - self.all_tags = [] + self.all_tags: List[Tag] = [] - def is_tag(self, tag_name: str) -> Optional[Tag]: + def is_tag(self, tag_name: str) -> Tag | None: """check if tag already exists""" for tag in self.all_tags: if tag._name == tag_name: diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 3b4c46c..0000000 --- a/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -toml~=0.10.2 -setuptools>=42.0 -black>=23.0.0 diff --git a/requirements_dev.txt b/requirements_dev.txt deleted file mode 100644 index 229e3af..0000000 --- a/requirements_dev.txt +++ /dev/null @@ -1,7 +0,0 @@ -flake8~=4.0.1 -pytest>=7.0.1 -pytest-cov~=2.0 -mypy~=0.910 -types-toml~=0.10.8.1 -pre-commit~=2.2.0 -tox diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index eba32fc..0000000 --- a/setup.cfg +++ /dev/null @@ -1,79 +0,0 @@ -[metadata] -name = refers -long_description = file: README.md -version = attr: refers.__version__ -description = Reference code simply -url = https://github.com/Stoops-ML/refers -author = Daniel Stoops -maintainer = Daniel Stoops -author_email = danielstoops25@gmail.com -maintainer_email = danielstoops25@gmail.com -platforms = unix, linux, osx, cygwin, win32 -classifiers = - Programming Language :: Python :: 3 - Programming Language :: Python :: 3.7 - Programming Language :: Python :: 3.8 - Programming Language :: Python :: 3.9 - Programming Language :: Python :: 3.10 - Programming Language :: Python :: 3.11 - Programming Language :: Python :: 3 :: Only - -[options] -packages= - refers -python_requires = >=3.7,<3.12 -package_dir = - =src -zip_safe = no - -[options.entry_points] -console_scripts = - refers = refers.cli:run - -[flake8] -max-line-length = 88 -extend-ignore = E203, E501, E402 -; TODO E402 only required for sys.path.append in setup.py. Once sys.path.append is removed then E402 can be removed from ignored list here - -[options.extras_require] -testing = - pytest-cov~=2.0 - flake8~=4.0.1 - pytest~=7.1.2 - mypy~=0.910 - types-toml~=0.10.8.1 - pre-commit~=2.2.0 - tox - -[tox:tox] -; run before tox in powershell (not inside pipenv shell): pyenv local 3.7.9, 3.8.10, 3.9.13, 3.10.10, 3.11.2 -envlist = - py37 - py38 - py39 - py310 - py311 - -[testenv] -setenv = - PYTHONPATH = {toxinidir} -deps = - -r{toxinidir}/requirements.txt - -r{toxinidir}/requirements_dev.txt -commands = - pytest --basetemp={envtmpdir} - -[testenv:py37] -base_python = python37 - -[testenv:py38] -base_python = python38 - -[testenv:py39] -base_python = python39 - -[testenv:py310] -base_python = python310 - -[testenv:py311] -base_python = python311 diff --git a/setup.py b/setup.py deleted file mode 100644 index a5eb29c..0000000 --- a/setup.py +++ /dev/null @@ -1,4 +0,0 @@ -from setuptools import setup # type: ignore - -if __name__ == "__main__": - setup() From 6595d03f0a817fe24e5ac5aff3a620f756f12def Mon Sep 17 00:00:00 2001 From: Daniel Stoops Date: Thu, 14 Mar 2024 13:29:29 +0200 Subject: [PATCH 2/4] Add types to poetry and mypy --- .pre-commit-config.yaml | 8 +++++++- pyproject.toml | 7 ++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ede0090..616c0e8 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -40,7 +40,7 @@ repos: always_run: true - id: mypy name: mypy - entry: mypy . + entry: mypy . --install-types --non-interactive language: system pass_filenames: false always_run: true @@ -50,6 +50,12 @@ repos: language: system pass_filenames: false always_run: true + - id: poetry types + name: poetry types + entry: poetry types update + language: system + pass_filenames: false + always_run: true - id: tox-check name: tox entry: tox diff --git a/pyproject.toml b/pyproject.toml index f9869c3..278cb10 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,6 +21,10 @@ pytest = "^8.1.1" reorder-python-imports = "^3.12.0" pytest-cov = "^4.1.0" flake8-pyproject = "^1.2.3" +poetry-types = "^0.5.0" + +[tool.poetry.group.types.dependencies] +types-toml = "^0.10.8.20240310" [build-system] requires = ["poetry-core"] @@ -78,7 +82,8 @@ deps = black>=24.2.0 commands = python -m flake8 refers/ tests/ - python -m mypy refers/ tests/ + python -m mypy --install-types --non-interactive + python -m mypy . python -m black --check --diff . """ From e42ed5cd14d2fc7e9e37f0141cb5e66d8ff5573c Mon Sep 17 00:00:00 2001 From: Daniel Stoops Date: Thu, 14 Mar 2024 13:29:56 +0200 Subject: [PATCH 3/4] Add flake8-pyproject to tox lint env --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 278cb10..23c9ef3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -78,6 +78,7 @@ commands = [testenv:lint] deps = flake8>=7.0.0 + flake8-pyproject>=1.2.3 mypy>=1.9.0 black>=24.2.0 commands = From 4160a3c2dd6c5fd5e117c1a202ab8dc0865a84f1 Mon Sep 17 00:00:00 2001 From: Daniel Stoops Date: Thu, 14 Mar 2024 13:33:30 +0200 Subject: [PATCH 4/4] Github actions checks only py3.11 --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9d71d5c..36ada03 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ jobs: strategy: matrix: os: [windows-latest] - python-version: ['3.6', '3.7', '3.8', '3.9', '3.10'] + python-version: ['3.11'] steps: - uses: actions/checkout@v2 @@ -23,4 +23,4 @@ jobs: python -m pip install --upgrade pip pip install tox tox-gh-actions - name: Test with tox - run: tox \ No newline at end of file + run: tox