From 2f6b38ab685d5279964b0d48863db47d2f5e66d5 Mon Sep 17 00:00:00 2001 From: Kartik Singhal Date: Tue, 26 Sep 2023 17:22:57 -0500 Subject: [PATCH] Several initial updates: - update packages - pytemplate/qtmlib -> pytket-phir - add mypy and ruff configs --- .gitignore | 169 +++++++++++++++++++++++++-- .pre-commit-config.yaml | 40 +++++-- Dockerfile | 6 +- README.md | 49 +------- docs/index.rst | 16 +-- mypy.ini | 50 ++++++++ pyproject.toml | 17 +-- pytemplate/__init__.py | 1 - pytemplate/main.py | 10 -- pytket/__init__.py | 0 pytket/phir/__init__.py | 1 + pytket/phir/main.py | 10 ++ {pytemplate => pytket/phir}/utils.py | 2 +- requirements.txt | 5 +- ruff.toml | 49 ++++++++ tests/__init__.py | 0 tests/test_main.py | 4 +- tests/test_utils.py | 6 +- 18 files changed, 323 insertions(+), 112 deletions(-) create mode 100644 mypy.ini delete mode 100644 pytemplate/__init__.py delete mode 100644 pytemplate/main.py create mode 100644 pytket/__init__.py create mode 100644 pytket/phir/__init__.py create mode 100644 pytket/phir/main.py rename {pytemplate => pytket/phir}/utils.py (67%) create mode 100644 ruff.toml create mode 100644 tests/__init__.py diff --git a/.gitignore b/.gitignore index a86b1d6..68bc17f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1,160 @@ -# qtmlib/.gitignore file contents +# Byte-compiled / optimized / DLL files __pycache__/ -*.pyc -*.pyo -*.egg-info/ -dist/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python build/ -.idea/ -.vscode/ -__pypackages__/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: *.log -*.swp -.DS_Store -.qtmlib/ -*.venv/ \ No newline at end of file +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 19f77d4..552da2e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,18 +1,44 @@ repos: - - repo: https://github.com/psf/black - rev: 23.7.0 + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.4.0 hooks: - - id: black + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-toml + - id: check-yaml + - id: check-added-large-files + # Python-specific + - id: check-ast + - id: check-docstring-first + - id: debug-statements + + - repo: https://github.com/crate-ci/typos + rev: v1.16.14 + hooks: + - id: typos - - repo: https://github.com/RobertCraigie/pyright-python - rev: v1.1.317 + - repo: https://github.com/psf/black + rev: 23.9.1 hooks: - - id: pyright + - id: black - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.0.278 + rev: v0.0.291 hooks: - id: ruff + args: [--fix, --exit-non-zero-on-fix] + + - repo: https://github.com/pre-commit/mirrors-mypy + rev: 'v1.5.1' + hooks: + - id: mypy + pass_filenames: false + args: [pytket/phir, tests] + additional_dependencies: [ + lark-parser==0.12.0, + pytest==7.4.2, + types-setuptools==68.2.0.0, + ] # Building docs on precommit seems like this will be slow. # - repo: https://github.com/pre-commit/mirrors-sphinx diff --git a/Dockerfile b/Dockerfile index 1225c17..8aa61ef 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ FROM python:3.11-slim-buster -# Set the working directory to /qtmlib -WORKDIR /qtmlib +# Set the working directory to /pytket-phir +WORKDIR /pytket-phir # Copy the pyproject.toml and requirements.txt files to the container COPY . . @@ -10,4 +10,4 @@ COPY . . RUN pip install --no-cache-dir -r requirements.txt # Start the web server -CMD ["python", "-m", "pytemplate.main"] \ No newline at end of file +CMD ["python", "-m", "pytket.phir.main"] diff --git a/README.md b/README.md index 7706cec..1a87117 100644 --- a/README.md +++ b/README.md @@ -1,51 +1,4 @@ -# pytemplate - -This is a Python 3.11 app called pytemplate. It uses toml instead of setup.py for configuration. The project includes Docker, Pyright, Ruff, GitHub Actions, Black, pre-commit, and Sphinx. - -## Project Structure - -The project structure is as follows: - -```sh -pytemplate -├── .github -│ └── workflows -│ └── python-app.yml -├── .pre-commit-config.yaml -├── .vscode -│ ├── launch.json -│ └── settings.json -├── docs -│ ├── conf.py -│ ├── index.rst -│ └── _static -├── qtmlib -│ ├── __init__.py -│ ├── main.py -│ └── utils.py -├── tests -│ ├── __init__.py -│ ├── test_main.py -│ └── test_utils.py -├── .dockerignore -├── .gitignore -├── Dockerfile -├── pyproject.toml -├── README.md -└── requirements.txt -``` - -The source code is located in the `pytemplate` folder, which contains the `__init__.py`, `main.py`, and `utils.py` files. The tests are located in the `tests` folder, which contains the `test_main.py` and `test_utils.py` files. - -The project uses toml for configuration instead of setup.py. The configuration file is located in `pyproject.toml`. - -The project includes Docker, with a Dockerfile located in the root directory. The `.dockerignore` file is also located in the root directory. - -The project includes Pyright for static type checking, pre-commit for code formatting, Black for code formatting and Ruff for linting. The configuration for these tools is located in the `.pre-commit-config.yaml` file. - -The project includes Sphinx for documentation, with the documentation located in the `docs` folder. The `conf.py` file contains the configuration for Sphinx. - -The project includes GitHub Actions for continuous integration, with the configuration located in the `.github/workflows/python-app.yml` file. +# pytket-phir ## Installation diff --git a/docs/index.rst b/docs/index.rst index fea026f..be909b2 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -1,4 +1,4 @@ -qtmlib +pytket-phir ====== .. toctree:: @@ -12,30 +12,30 @@ qtmlib Installation ------------ -To install qtmlib, simply run: +To install pytket-phir, simply run: .. code-block:: bash - pip install qtmlib + pip install pytket-phir Usage ----- -To use qtmlib, import the main module: +To use pytket-phir, import the main module: .. code-block:: python - from qtmlib import main + from pytket-phir import main main.run() Utils ----- -The utils module contains various utility functions that can be used in conjunction with qtmlib. To use the utils module, import it like so: +The utils module contains various utility functions that can be used in conjunction with pytket-phir. To use the utils module, import it like so: .. code-block:: python - from qtmlib import utils + from pytket-phir import utils - utils.do_something() \ No newline at end of file + utils.do_something() diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 0000000..27cef6f --- /dev/null +++ b/mypy.ini @@ -0,0 +1,50 @@ +# Global options: + +; See doc at https://mypy.readthedocs.io/en/stable/config_file.html +[mypy] +follow_imports = silent +python_version = 3.10 + +exclude = (?x)( + ^build/ + ) + +## Disallow dynamic typing +disallow_any_unimported = true +disallow_any_expr = true +disallow_any_decorated = true +disallow_any_explicit = true +disallow_any_generics = true +disallow_subclassing_any = true + +## Untyped definitions and calls +disallow_untyped_calls = true +disallow_untyped_defs = true +disallow_incomplete_defs = true +check_untyped_defs = true +disallow_untyped_decorators = true + +## Configuring warnings +warn_redundant_casts = true +warn_unused_ignores = true +warn_return_any = true +; warn_unreachable = true # disabled because it can result in false positives + +## Miscellaneous strictness flags +local_partial_types = true +implicit_reexport = false +strict_equality = true +strict = true + +## Configuring error messages +; show_error_context = true +pretty = true + +## Miscellaneous +scripts_are_modules = true +warn_unused_configs = true + +# Per-module options: + +[mypy-tests.*] +disallow_untyped_defs = false diff --git a/pyproject.toml b/pyproject.toml index 0922809..758a596 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,32 +3,19 @@ requires = ["setuptools", "wheel"] build-backend = "setuptools.build_meta" [project] -name = "pytemplate" +name = "pytket-phir" version = "0.0.1" description = "A python library" -requires-python = ">=3.11" +requires-python = ">=3.10" authors = [{name = "Author", email = "author@email.com" }] [tool.setuptools.packages.find] where = ["."] -[tool.ruff] -# See https://beta.ruff.rs/docs/rules/ -select = ["E", "F", "B", "RUF", "PT", "UP", "C4", "D"] -extend-exclude = ["**/*.ipynb"] -target-version = "py311" - [tool.ruff.pydocstyle] # Use Google-style docstrings. convention = "google" -# See https://microsoft.github.io/pyright/#/getting-started -[tool.pyright] -include = ["pytemplate","tests"] -ignore = ["**/*.ipynb"] -pythonVersion = "3.11" -typeCheckingMode = "strict" - [tool.pytest.ini_options] pythonpath = [ "." diff --git a/pytemplate/__init__.py b/pytemplate/__init__.py deleted file mode 100644 index 491c274..0000000 --- a/pytemplate/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Init file for qtmlib.""" diff --git a/pytemplate/main.py b/pytemplate/main.py deleted file mode 100644 index 40ced22..0000000 --- a/pytemplate/main.py +++ /dev/null @@ -1,10 +0,0 @@ -"""This is the main module of the qtmlib package.""" - - -def hello_world(): - """Print 'Hello, world!' to the console.""" - hw = "Hello, World!" - return hw - - -print(hello_world()) diff --git a/pytket/__init__.py b/pytket/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pytket/phir/__init__.py b/pytket/phir/__init__.py new file mode 100644 index 0000000..393be07 --- /dev/null +++ b/pytket/phir/__init__.py @@ -0,0 +1 @@ +"""Init file for pytket-phir.""" diff --git a/pytket/phir/main.py b/pytket/phir/main.py new file mode 100644 index 0000000..bca21dd --- /dev/null +++ b/pytket/phir/main.py @@ -0,0 +1,10 @@ +"""This is the main module of the pytket-phir package.""" + + +def hello_world() -> str: + """Print 'Hello, world!' to the console.""" + hw = "Hello, World!" + return hw + + +print(hello_world()) # noqa: T201 diff --git a/pytemplate/utils.py b/pytket/phir/utils.py similarity index 67% rename from pytemplate/utils.py rename to pytket/phir/utils.py index 38bc7fc..4114ecb 100644 --- a/pytemplate/utils.py +++ b/pytket/phir/utils.py @@ -1,4 +1,4 @@ -"""Utility functions for the qtmlib package.""" +"""Utility functions for the pytket-phir package.""" def add_numbers(a: int, b: int) -> int: diff --git a/requirements.txt b/requirements.txt index 37ef5e4..26a896a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,8 @@ black +build +lark-parser==0.12.0 +mypy pre-commit -pyright pytest -pytest-lazy-fixture sphinx wheel diff --git a/ruff.toml b/ruff.toml new file mode 100644 index 0000000..825b5ed --- /dev/null +++ b/ruff.toml @@ -0,0 +1,49 @@ +target-version = "py310" + +line-length = 88 + +select = [ + "E", # pycodestyle Errors + "W", # pycodestyle Warnings + + "A", # flake8-builtins + "B", # flake8-Bugbear + "BLE", # flake8-blind-except + "C4", # flake8-comprehensions + "COM", # flake8-commas + "EM", # flake8-errmsg + "EXE", # flake8-executable + "F", # pyFlakes + "FA", # flake8-future-annotations + "FIX", # flake8-fixme + "FLY", # flynt + "G", # flake8-logging-format + "I", # isort + "INP", # flake8-no-pep420 + "ISC", # flake8-implicit-str-concat + "N", # pep8-Naming + "NPY", # NumPy-specific + "PERF", # Perflint + "PGH", # pygrep-hooks + "PIE", # flake8-pie + "PL", # pylint + "PT", # flake8-pytest-style + "RSE", # flake8-raise + "RUF", # Ruff-specific + "S", # flake8-bandit (Security) + "SIM", # flake8-simplify + "SLF", # flake8-self + "SLOT", # flake8-slots + "T10", # flake8-debugger + "T20", # flake8-print + "TD", # flake8-todos + "TRY", # tryceratops + "UP", # pyupgrade + "YTT", # flake8-2020 +] + +ignore = [] + +[per-file-ignores] +"__init__.py" = ["F401"] # module imported but unused +"tests/*" = ["S101"] # Use of `assert` detected diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_main.py b/tests/test_main.py index 01b3f34..9d673cb 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,6 +1,6 @@ -"""Tests for qtmlib.main module.""" +"""Tests for pytket-phir.main module.""" -from pytemplate.main import hello_world +from pytket.phir.main import hello_world def test_hello_world(): diff --git a/tests/test_utils.py b/tests/test_utils.py index 378ed6b..d181a80 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,10 +1,10 @@ -"""Tests for qtmlib.utils.""" +"""Tests for pytket-phir.utils.""" -from pytemplate.utils import add_numbers +from pytket.phir.utils import add_numbers def test_add(): """Test the add function.""" - assert add_numbers(2, 3) == 5 + assert add_numbers(2, 3) == 5 # noqa: PLR2004 assert add_numbers(0, 0) == 0 assert add_numbers(-1, 1) == 0