jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1166821?usp=email )
Change subject: [tests] make mypy tests mandatory for some files
......................................................................
[tests] make mypy tests mandatory for some files
- some files passes the mypy typing linter and they should pass the
tests now with pre-commit tests. Therefore add mirrors-mypy to
pre-commit tests and specify the files to be tested
- the non-voting test is kept for the remaining files. Therefore
use conftest.py to exclude the files tested by pre-commit
- use Python 3.9 for mypy tests
Bug: T398947
Change-Id: Ifbe70e9e256710de1e3f4029ae556ed9cd03f6ba
---
M .pre-commit-config.yaml
M CONTENT.rst
A conftest.py
M pyproject.toml
M tox.ini
5 files changed, 77 insertions(+), 2 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 0b74aac..b64ec46 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -104,6 +104,7 @@
- id: flake8
args:
- --doctests
+ - --config=tox.ini
additional_dependencies:
# Due to incompatibilities between packages the order matters.
- flake8-bugbear>=24.12.12
@@ -111,3 +112,21 @@
- flake8-print>=5.0.0
- flake8-tuple>=0.4.1
- pep8-naming>=0.15.1
+ - repo: https://github.com/pre-commit/mirrors-mypy
+ rev: v1.16.1
+ hooks:
+ - id: mypy
+ args:
+ - --config-file=pyproject.toml
+ - --follow-imports=silent
+ # Test for files which already passed in past.
+ # They should be also used in conftest.py to exclude them from
non-voting mypy test.
+ files: >
+ ^pywikibot/(__metadata__|exceptions|fixes|time)\.py$|
+ ^pywikibot/(comms|data|families|specialbots)/__init__\.py$|
+ ^pywikibot/families/[a-z]+_family\.py$|
+ ^pywikibot/page/(__init__|_decorators|_revision)\.py$|
+ ^pywikibot/scripts/(?:i18n/)?__init__\.py$|
+
^pywikibot/site/(__init__|_basesite|_decorators|_interwikimap|_upload)\.py$|
+ ^pywikibot/tools/(_logging|_unidata|formatter)\.py$|
+
^pywikibot/userinterfaces/(__init__|_interface_base|terminal_interface)\.py$
diff --git a/CONTENT.rst b/CONTENT.rst
index 50936af..5ffc998 100644
--- a/CONTENT.rst
+++ b/CONTENT.rst
@@ -24,6 +24,8 @@
+---------------------------+-----------------------------------------------------------+
| ROADMAP.rst | PyPI version roadmap file
|
+---------------------------+-----------------------------------------------------------+
+ | conftest.py | Local per-directory plugin for pytest-mypy
|
+
+---------------------------+-----------------------------------------------------------+
| dev-requirements.txt | PIP requirements file for development
dependencies |
+---------------------------+-----------------------------------------------------------+
| make_dist.py | Script to create a Pywikibot distribution
|
diff --git a/conftest.py b/conftest.py
new file mode 100644
index 0000000..8013102
--- /dev/null
+++ b/conftest.py
@@ -0,0 +1,52 @@
+"""Configuration file for pytest."""
+#
+# (C) Pywikibot team, 2025
+#
+# Distributed under the terms of the MIT license.
+#
+from __future__ import annotations
+
+import re
+from pathlib import Path
+from typing import Literal
+
+
+EXCLUDE_PATTERN = re.compile(
+ r'(?:'
+ r'(__metadata__|exceptions|fixes|time)|'
+ r'(comms|data|families|specialbots)/__init__|'
+ r'families/[a-z]+_family|'
+ r'page/(__init__|_decorators|_revision)|'
+ r'scripts/(i18n/)?__init__|'
+ r'site/(__init__|_basesite|_decorators|_interwikimap|_upload)|'
+ r'tools/(_logging|_unidata|formatter)|'
+ r'userinterfaces/(__init__|_interface_base|terminal_interface)'
+ r')\.py'
+)
+
+
+def pytest_ignore_collect(collection_path: Path,
+ config) -> Literal[True] | None:
+ """Ignore files matching EXCLUDE_PATTERN when pytest-mypy is loaded.
+
+ .. versionadded:: 10.3
+ """
+ # Check if any plugin name includes 'mypy'
+ plugin_names = {p.__class__.__name__.lower()
+ for p in config.pluginmanager.get_plugins()}
+ if not any('mypy' in name for name in plugin_names):
+ return None
+
+ project_root = Path(__file__).parent / 'pywikibot'
+ try:
+ rel_path = collection_path.relative_to(project_root)
+ except ValueError:
+ # Ignore files outside project root
+ return None
+
+ norm_path = rel_path.as_posix()
+ if EXCLUDE_PATTERN.fullmatch(norm_path):
+ print(f'Ignoring file in mypy: {norm_path}') # noqa: T201
+ return True
+
+ return None
diff --git a/pyproject.toml b/pyproject.toml
index cea7b27..8ff8cc9 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -177,7 +177,7 @@
[tool.mypy]
-python_version = 3.8
+python_version = 3.9
enable_error_code = [
"ignore-without-code",
]
diff --git a/tox.ini b/tox.ini
index 0c859e3..6df5ee2 100644
--- a/tox.ini
+++ b/tox.ini
@@ -61,7 +61,7 @@
deeptest-py312: pytest-subtests != 0.14.0
[testenv:typing]
-basepython = python3.8
+basepython = python3.9
deps = pytest-mypy
commands =
mypy --version
@@ -222,9 +222,11 @@
# pep8-naming
classmethod-decorators = classmethod,classproperty
+
[pycodestyle]
exclude = .tox,.git,./*.egg,build,./scripts/i18n/*
+
[pytest]
minversion = 7.0.1
testpaths = tests
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1166821?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ifbe70e9e256710de1e3f4029ae556ed9cd03f6ba
Gerrit-Change-Number: 1166821
Gerrit-PatchSet: 22
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]