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

Basic mypy typings #786

Merged
merged 22 commits into from Aug 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f57ac02
ci(typings): Add mypy
tony Aug 14, 2022
954ef41
chore(util): Use console_to_str from libtmux
tony Aug 14, 2022
59ba779
refactor!(_compat): Remove console_to_str
tony Aug 14, 2022
b08e8b5
chore(mypy): Override untyped libs
tony Jul 24, 2022
2a8f972
build(deps): Add types-docutils
tony Jul 24, 2022
844f8f7
build(mypy): Allow docutils again
tony Jul 24, 2022
5031c62
build(deps): Add colorama typings
tony Jul 24, 2022
06da457
chore(test_cli): Add typings for mypy
tony Jul 25, 2022
af55803
chore(conftest): Basic Typings
tony Aug 14, 2022
e8bf689
refactor(test fixtures): Use basic dataclass
tony Aug 14, 2022
c20dfc0
refactor(aafig): Use hashlib library (sha1 deprecated since 2.5)
tony Aug 14, 2022
73e3b09
refactor(aafig): Fix import location (pyright)
tony Aug 14, 2022
d3a9a92
chore(aafig): Import from logging from standard library
tony Aug 14, 2022
bed670d
docs(conf): Basic typings
tony Aug 14, 2022
e3e6472
chore(command_freeze): Fix odd type / cycling issue caused by import
tony Aug 14, 2022
921e715
refactor(shell): Import fix
tony Aug 14, 2022
98b029d
chore(breakpoint): Ignore workaround
tony Aug 14, 2022
8ef49a7
refactor!(logging): mypy updates
tony Aug 14, 2022
ca09e80
refactor(logging): Remove protocol for now, to avoid typing-extensions
tony Aug 14, 2022
5400783
chore(setup.cfg): Move pytest config down for doc example
tony Aug 14, 2022
04317db
docs(developing): Note flake8 and mypy
tony Aug 14, 2022
f130e9b
docs(CHANGES): Add basic type annotations
tony Aug 14, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/workflows/tests.yml
Expand Up @@ -90,9 +90,13 @@ jobs:
- name: Install python dependencies
run: |
poetry install -E "test coverage lint"

- name: Lint with flake8
run: |
poetry run flake8
run: poetry run flake8

- name: Lint with mypy
run: poetry run mypy .

- name: Test with pytest
continue-on-error: ${{ matrix.tmux-version == 'master' }}
run: |
Expand Down
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -24,6 +24,7 @@ $ pipx install --suffix=@next 'tmuxp' --pip-args '\--pre' --force
- libtmux updated from v0.12 to v0.14 {issue}`790`
- Add [doctest](https://docs.python.org/3/library/doctest.html) w/
[pytest + doctest](https://docs.pytest.org/en/7.1.x/how-to/doctest.html), ({issue}`791`).
- Added basic [mypy](http://mypy-lang.org/) type annotations via {issue}`786`

## tmuxp 1.12.1 (2022-08-04)

Expand Down
10 changes: 3 additions & 7 deletions docs/_ext/aafig.py
Expand Up @@ -10,19 +10,15 @@
:author: Leandro Lucarella <llucax@gmail.com>
:license: BOLA, see LICENSE for details
"""
import logging
import posixpath
from hashlib import sha1 as sha
from os import path

from docutils import nodes
from docutils.parsers.rst.directives import flag, images, nonnegative_int
from sphinx.errors import SphinxError
from sphinx.util import ensuredir, logging, relative_uri

try:
from hashlib import sha1 as sha
except ImportError:
from sha import sha

from sphinx.util.osutil import ensuredir, relative_uri

try:
import aafigure
Expand Down
7 changes: 4 additions & 3 deletions docs/conf.py
@@ -1,6 +1,7 @@
# flake8: NOQA E5
import inspect
import sys
import typing as t
from os.path import dirname, relpath
from pathlib import Path

Expand All @@ -14,7 +15,7 @@
sys.path.insert(0, str(cwd / "_ext"))

# package data
about = {}
about: t.Dict[str, str] = {}
with open(project_root / "tmuxp" / "__about__.py") as fp:
exec(fp.read(), about)

Expand Down Expand Up @@ -61,8 +62,8 @@
html_static_path = ["_static"]
html_favicon = "_static/favicon.ico"
html_theme = "furo"
html_theme_path = []
html_theme_options = {
html_theme_path: t.List[str] = []
html_theme_options: t.Dict[str, t.Union[str, t.List[t.Dict[str, str]]]] = {
"light_logo": "img/tmuxp.svg",
"dark_logo": "img/tmuxp.svg",
"footer_icons": [
Expand Down
104 changes: 104 additions & 0 deletions docs/developing.md
Expand Up @@ -334,6 +334,106 @@ this will load the `.tmuxp.yaml` in the root of the project.

```

## Formatting

The project uses [black] and [isort] (one after the other). Configurations are in `pyproject.toml`
and `setup.cfg`:

- `make black isort`: Run `black` first, then `isort` to handle import nuances

## Linting

[flake8] and [mypy] run via CI in our GitHub Actions. See the configuration in `pyproject.toml` and
`setup.cfg`.

### flake8

[flake8] provides fast, reliable, barebones styling and linting.

````{tab} Command

poetry:

```console
$ poetry run flake8
```

If you setup manually:

```console
$ flake8
```

````

````{tab} make

```console
$ make flake8
```

````

````{tab} Watch

```console
$ make watch_flake8
```

requires [`entr(1)`].

````

````{tab} Configuration

See `[flake8]` in setup.cfg.

```{literalinclude} ../setup.cfg
:language: ini
:start-at: "[flake8]"
:end-before: "[isort]"

```

````

### mypy

[mypy] is used for static type checking.

````{tab} Command

poetry:

```console
$ poetry run mypy .
```

If you setup manually:

```console
$ mypy .
```

````

````{tab} make

```console
$ make mypy
```

````

````{tab} Watch

```console
$ make watch_mypy
```

requires [`entr(1)`].
````

(gh-actions)=

## Continuous integration
Expand All @@ -350,6 +450,10 @@ the [gh build site].
[py.test usage argument]: https://pytest.org/latest/usage.html
[entr]: http://entrproject.org/
[`entr(1)`]: http://entrproject.org/
[black]: https://github.com/psf/black
[isort]: https://pypi.org/project/isort/
[flake8]: https://flake8.pycqa.org/
[mypy]: http://mypy-lang.org/
[github actions]: https://github.com/features/actions
[gh build site]: https://github.com/tmux-python/tmuxp/actions?query=workflow%3Atests
[.github/workflows/tests.yml]: https://github.com/tmux-python/tmuxp/blob/master/.github/workflows/tests.yml
Expand Down
26 changes: 25 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion pyproject.toml
Expand Up @@ -86,6 +86,8 @@ isort = "*"
### Lint ###
flake8 = "*"
mypy = "*"
types-colorama = "^0.4.15"
types-docutils = "^0.19.0"

[tool.poetry.extras]
docs = [
Expand All @@ -107,8 +109,20 @@ docs = [
test = ["pytest", "pytest-rerunfailures", "pytest-mock", "pytest-watcher"]
coverage = ["codecov", "coverage", "pytest-cov"]
format = ["black", "isort"]
lint = ["flake8", "mypy"]
lint = ["flake8", "mypy", "types-colorama", "types-docutils"]

[build-system]
requires = ["poetry_core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[[tool.mypy.overrides]]
module = [
"kaptan",
"aafigure",
"libtmux.*",
"IPython.*",
"ptpython.*",
"prompt_toolkit.*",
"bpython"
]
ignore_missing_imports = true
12 changes: 6 additions & 6 deletions setup.cfg
Expand Up @@ -5,12 +5,6 @@ max-line-length = 88
# Stuff we ignore thanks to black: https://github.com/ambv/black/issues/429
extend-ignore = E203,W503

[tool:pytest]
filterwarnings =
ignore:distutils Version classes are deprecated. Use packaging.version instead.
addopts = --reruns=0 --tb=short --no-header --showlocals --doctest-modules
doctest_optionflags = ELLIPSIS NORMALIZE_WHITESPACE

[isort]
profile = black
combine_as_imports= true
Expand All @@ -21,3 +15,9 @@ known_pytest = pytest,py
known_first_party = libtmux,tmuxp
sections = FUTURE,STDLIB,PYTEST,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
line_length = 88

[tool:pytest]
filterwarnings =
ignore:distutils Version classes are deprecated. Use packaging.version instead.
addopts = --reruns=0 --tb=short --no-header --showlocals --doctest-modules
doctest_optionflags = ELLIPSIS NORMALIZE_WHITESPACE
13 changes: 13 additions & 0 deletions tests/fixtures/structures.py
@@ -0,0 +1,13 @@
import dataclasses
import typing as t


@dataclasses.dataclass
class TestConfigData:
expand1: t.Any
expand2: t.Any
expand_blank: t.Any
sampleconfig: t.Any
shell_command_before: t.Any
shell_command_before_session: t.Any
trickle: t.Any
3 changes: 2 additions & 1 deletion tests/test_cli.py
Expand Up @@ -2,6 +2,7 @@
import json
import os
import pathlib
import typing as t
from unittest.mock import MagicMock

import pytest
Expand Down Expand Up @@ -416,7 +417,7 @@ def test_regression_00132_session_name_with_dots(
):
yaml_config = FIXTURE_PATH / "workspacebuilder" / "regression_00132_dots.yaml"
cli_args = [str(yaml_config)]
inputs = []
inputs: t.List[str] = []
runner = CliRunner()
result = runner.invoke(
cli.command_load, cli_args, input="".join(inputs), standalone_mode=False
Expand Down