Skip to content

Commit a7f8bbc

Browse files
Merge pull request #279 from jakub-nt/pytest-legacy-python
Fix Pytest using the wrong Python installation, add Pyright to legacy Python workflow, fix syntax errors on Python 3.5, remove Pyflakes
2 parents 0723f73 + 645e704 commit a7f8bbc

File tree

6 files changed

+72
-42
lines changed

6 files changed

+72
-42
lines changed

.github/actions/set-up-legacy-python/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ runs:
2222
command: pip install -U pip
2323
- name: Install dependencies
2424
run: |
25-
python -m pip install flake8 pytest setuptools wheel
25+
python -m pip install flake8 pyright pytest setuptools wheel
2626
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
2727
shell: bash

.github/workflows/python-tests.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,14 @@ jobs:
3030
- name: Install dependencies
3131
run: |
3232
python -m pip install --upgrade pip
33-
python -m pip install flake8 pyright pyflakes pytest setuptools wheel
33+
python -m pip install flake8 pyright pytest setuptools wheel
3434
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
3535
- name: Lint with flake8
3636
run: |
3737
flake8 . --ignore=E203,W503,E722,E731 --max-complexity=100 --max-line-length=160
3838
- name: Lint with pyright (type checking)
3939
run: |
4040
pyright cfbs
41-
- name: Lint with pyflakes
42-
run: |
43-
pyflakes cfbs
4441
- name: Test with pytest
4542
run: |
4643
pytest
@@ -73,10 +70,13 @@ jobs:
7370
python-version: ${{ matrix.python-version }}
7471
- name: Lint with flake8
7572
run: |
76-
flake8 . --ignore=E203,W503,E722,E731 --max-complexity=100 --max-line-length=160
73+
python -m flake8 . --ignore=E203,W503,E722,E731 --max-complexity=100 --max-line-length=160
74+
- name: Lint with pyright (type checking)
75+
run: |
76+
python -m pyright cfbs
7777
- name: Test with pytest
7878
run: |
79-
pytest
79+
python -m pytest
8080
- name: Install
8181
run: |
8282
python setup.py sdist bdist_wheel

cfbs/analyze.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ def checksums_files(
7373
files_dir_path,
7474
checksums_dict=None,
7575
files_dict=None,
76-
ignored_path_components=[],
76+
ignored_path_components=None,
7777
):
7878
if checksums_dict is None:
7979
checksums_dict = copy.deepcopy(DEFAULT_CHECKSUMS_DICT)
8080
if files_dict is None:
8181
files_dict = copy.deepcopy(DEFAULT_FILES_DICT)
82+
if ignored_path_components is None:
83+
ignored_path_components = []
8284

8385
for root, _, files in os.walk(files_dir_path):
8486
for name in files:
@@ -230,7 +232,7 @@ def mpf_normalized_path(path, is_parentpath: bool, masterfiles_dir):
230232
`path` should be a path inside the masterfiles directory (or inside the parent directory, if `is_parentpath` is `True`).
231233
"""
232234
# downloaded MPF release information filepaths always have forward slashes
233-
norm_path = path.replace(os.sep, "/")
235+
norm_path = path.replace(os.sep, "/") # type: str
234236

235237
if is_parentpath:
236238
if norm_path.startswith(masterfiles_dir + "/"):

cfbs/args.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import argparse
22
import os
3-
from typing import List, Union
3+
from typing import List, Optional # noqa: F401
44

55
from cfbs import commands
66
from cfbs.utils import cache, CFBSExitError
@@ -9,31 +9,33 @@
99
class ArgsTypesNamespace(argparse.Namespace):
1010
"""Manual type hints to args attributes"""
1111

12-
command: Union[str, None]
13-
args: List[str]
14-
loglevel: str
15-
manual: bool
16-
version: bool
17-
force: bool
18-
non_interactive: bool
19-
index: Union[str, None]
20-
check: bool
21-
checksum: Union[str, None]
22-
keep_order: bool
23-
git: Union[bool, None]
24-
git_user_name: Union[str, None]
25-
git_user_email: Union[str, None]
26-
git_commit_message: Union[str, None]
27-
ignore_versions_json: bool
28-
omit_download: bool
29-
check_against_git: bool
30-
minimum_version: Union[str, None]
31-
to_json: Union[str, None]
32-
reference_version: Union[str, None]
33-
masterfiles_dir: Union[str, None]
34-
ignored_path_components: List[str]
35-
offline: bool
36-
masterfiles: Union[str, None]
12+
# PEP 484 style type hints for compatibility with Python 3.5.
13+
# This commit can be reverted, and type hints returned to the PEP 526 style type hints, once the supported Python version becomes 3.6+.
14+
command = None # type: Optional[str]
15+
args = [] # type: List[str]
16+
loglevel = "warning" # type: str
17+
manual = False # type: bool
18+
version = False # type: bool
19+
force = False # type: bool
20+
non_interactive = False # type: bool
21+
index = None # type: Optional[str]
22+
check = False # type: bool
23+
checksum = None # type: Optional[str]
24+
keep_order = False # type: bool
25+
git = None # type: Optional[bool]
26+
git_user_name = None # type: Optional[str]
27+
git_user_email = None # type: Optional[str]
28+
git_commit_message = None # type: Optional[str]
29+
ignore_versions_json = False # type: bool
30+
omit_download = False # type: bool
31+
check_against_git = False # type: bool
32+
minimum_version = None # type: Optional[str]
33+
to_json = None # type: Optional[str]
34+
reference_version = None # type: Optional[str]
35+
masterfiles_dir = None # type: Optional[str]
36+
ignored_path_components = None # type: Optional[List[str]]
37+
offline = False # type: bool
38+
masterfiles = None # type: Optional[str]
3739

3840

3941
def get_args():

cfbs/cfbs_types.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
1-
from typing import List, NamedTuple, Union
1+
from typing import List, NamedTuple, Optional
22

33
CFBSCommandExitCode = int
44

55

6-
class CFBSCommandGitResult(NamedTuple):
7-
return_code: int
8-
do_commit: bool = True
9-
commit_message: Union[str, None] = None
10-
commit_files: List[str] = []
6+
# The usual, not `class`-based, method of defining a `typing.NamedTuple` type does not support default values,
7+
# and it seems that the `class`-based method's constructor does not work with the Python 3.5 PEP 484[1] comment type hints.
8+
# Thus, for compatibility with Python 3.5, a more verbose definition of `CFBSCommandGitResult` is used.
9+
# This commit can be reverted, and type hints returned to the PEP 526 style type hints[2], once the supported Python version becomes 3.6+.
10+
# References:
11+
# [1]: https://peps.python.org/pep-0484/#type-comments
12+
# [2]: https://peps.python.org/pep-0526/#class-and-instance-variable-annotations
13+
_CFBSCommandGitResult = NamedTuple(
14+
"_CFBSCommandGitResult",
15+
[
16+
("return_code", int),
17+
("do_commit", bool),
18+
("commit_message", Optional[str]),
19+
("commit_files", List[str]),
20+
],
21+
)
22+
23+
24+
class CFBSCommandGitResult(_CFBSCommandGitResult):
25+
def __new__(
26+
cls,
27+
return_code: int,
28+
do_commit: bool = True,
29+
commit_message: Optional[str] = None,
30+
commit_files: Optional[List[str]] = None,
31+
):
32+
if commit_files is None:
33+
commit_files = []
34+
return super(CFBSCommandGitResult, cls).__new__(
35+
cls, return_code, do_commit, commit_message, commit_files
36+
)

cfbs/commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def init_command(
205205
"description": description,
206206
"build": [],
207207
}
208-
)
208+
) # type: OrderedDict[str, str | List | bool]
209209
if index:
210210
config["index"] = index
211211

0 commit comments

Comments
 (0)