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

tests: Enable mypy type checking in pre-commit and fix typing issues #1581

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
10 changes: 10 additions & 0 deletions .mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[mypy]
warn_unused_configs = True
check_untyped_defs=True

[mypy-gi.*]
ignore_missing_imports = True
[mypy-dbus.*]
ignore_missing_imports = True
[mypy-dbusmock.*]
ignore_missing_imports = True
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ repos:
# Run the formatter.
- id: ruff-format
args: [ --check ]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.14.1
hooks:
- id: mypy
additional_dependencies:
- pytest
6 changes: 3 additions & 3 deletions doc/copy-subdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import sys
import shutil

subdir = os.getenv("MESON_SUBDIR")
subdir = os.environ["MESON_SUBDIR"]

input_path = os.path.join(os.getenv("MESON_SOURCE_ROOT"), subdir, sys.argv[1])
output_path = os.path.join(os.getenv("MESON_BUILD_ROOT"), subdir, sys.argv[2])
input_path = os.path.join(os.environ["MESON_SOURCE_ROOT"], subdir, sys.argv[1])
output_path = os.path.join(os.environ["MESON_BUILD_ROOT"], subdir, sys.argv[2])

os.makedirs(os.path.dirname(output_path), exist_ok=True)

Expand Down
6 changes: 3 additions & 3 deletions doc/copy-subtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import sys
import shutil

subdir = os.getenv("MESON_SUBDIR")
subdir = os.environ["MESON_SUBDIR"]

input_path = os.path.join(os.getenv("MESON_SOURCE_ROOT"), subdir, sys.argv[1])
output_path = os.path.join(os.getenv("MESON_BUILD_ROOT"), subdir, sys.argv[2])
input_path = os.path.join(os.environ["MESON_SOURCE_ROOT"], subdir, sys.argv[1])
output_path = os.path.join(os.environ["MESON_BUILD_ROOT"], subdir, sys.argv[2])

os.makedirs(os.path.dirname(output_path), exist_ok=True)

Expand Down
2 changes: 1 addition & 1 deletion doc/fix-rst-dbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ def fix_title_template_string(lines):
fix_title_template_string(lines)

with open(fullpath, "w") as f:
lines = f.writelines(lines)
f.writelines(lines)
15 changes: 0 additions & 15 deletions tests/.mypy.ini

This file was deleted.

5 changes: 3 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,10 @@ def templates(
dbusmock.BusType.SYSTEM: {},
dbusmock.BusType.SESSION: {},
}
for template, params in required_templates.items():
for template_data, params in required_templates.items():
template, bus_name = (template_data.split(":") + [None])[:2]
assert template
params = template_params.get(template, params)
template, bus_name = (template.split(":") + [None])[:2]
_start_template(busses, template, bus_name, params)
yield
_terminate_servers(busses)
Expand Down
11 changes: 7 additions & 4 deletions tests/templates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,25 @@ def respond(
) -> None:
def reply():
nonlocal response
res = None
logger.debug(f"Request {self.handle}: trying to reply")
if callable(response):
try:
response = response()
res = response()
except Exception as e:
logger.critical(
f"Request {self.handle}: failed getting response: {e}"
)
self.cb_error(e)
self._unexport()
return
else:
res = response

assert response
logger.debug(f"Request {self.handle}: replying {response}")
assert res
logger.debug(f"Request {self.handle}: replying {res}")

self.cb_success(response.response, response.results)
self.cb_success(res.response, res.results)
self._unexport()

if done_cb:
Expand Down
1 change: 1 addition & 0 deletions tests/templates/access.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is formatted with Python Black
# mypy: disable-error-code="misc"

from tests.templates import Response, init_logger, ImplRequest

Expand Down
1 change: 1 addition & 0 deletions tests/templates/account.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is formatted with Python Black
# mypy: disable-error-code="misc"

from tests.templates import Response, init_logger, ImplRequest

Expand Down
1 change: 1 addition & 0 deletions tests/templates/appchooser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is formatted with Python Black
# mypy: disable-error-code="misc"

from tests.templates import Response, init_logger, ImplRequest

Expand Down
1 change: 1 addition & 0 deletions tests/templates/background.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is formatted with Python Black
# mypy: disable-error-code="misc"

from tests.templates import init_logger

Expand Down
1 change: 1 addition & 0 deletions tests/templates/clipboard.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is formatted with Python Black
# mypy: disable-error-code="misc"

from tests.templates import init_logger

Expand Down
1 change: 1 addition & 0 deletions tests/templates/dynamiclauncher.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is formatted with Python Black
# mypy: disable-error-code="misc"

from tests.templates import Response, init_logger, ImplRequest

Expand Down
1 change: 1 addition & 0 deletions tests/templates/email.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is formatted with Python Black
# mypy: disable-error-code="misc"

from tests.templates import Response, init_logger, ImplRequest

Expand Down
1 change: 1 addition & 0 deletions tests/templates/filechooser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is formatted with Python Black
# mypy: disable-error-code="misc"

from tests.templates import Response, init_logger, ImplRequest

Expand Down
1 change: 1 addition & 0 deletions tests/templates/geoclue2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
# mypy: disable-error-code="misc"

from tests.templates import init_logger

Expand Down
1 change: 1 addition & 0 deletions tests/templates/globalshortcuts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is formatted with Python Black
# mypy: disable-error-code="misc"

from tests.templates import Response, init_logger, ImplRequest, ImplSession

Expand Down
1 change: 1 addition & 0 deletions tests/templates/inhibit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is formatted with Python Black
# mypy: disable-error-code="misc"

from tests.templates import Response, init_logger, ImplRequest, ImplSession

Expand Down
1 change: 1 addition & 0 deletions tests/templates/inputcapture.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is formatted with Python Black
# mypy: disable-error-code="misc"

from tests.templates import Response, init_logger

Expand Down
1 change: 1 addition & 0 deletions tests/templates/lockdown.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is formatted with Python Black
# mypy: disable-error-code="misc"

from tests.templates import init_logger

Expand Down
1 change: 1 addition & 0 deletions tests/templates/notification.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is formatted with Python Black
# mypy: disable-error-code="misc"

from tests.templates import init_logger

Expand Down
1 change: 1 addition & 0 deletions tests/templates/print.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is formatted with Python Black
# mypy: disable-error-code="misc"

from tests.templates import Response, init_logger, ImplRequest

Expand Down
1 change: 1 addition & 0 deletions tests/templates/remotedesktop.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is formatted with Python Black
# mypy: disable-error-code="misc"

from tests.templates import Response, init_logger, ImplRequest, ImplSession

Expand Down
1 change: 1 addition & 0 deletions tests/templates/screenshot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is formatted with Python Black
# mypy: disable-error-code="misc"

from tests.templates import Response, init_logger, ImplRequest

Expand Down
1 change: 1 addition & 0 deletions tests/templates/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is formatted with Python Black
# mypy: disable-error-code="misc"

from tests.templates import init_logger

Expand Down
1 change: 1 addition & 0 deletions tests/templates/usb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is formatted with Python Black
# mypy: disable-error-code="misc"

from tests.templates import Response, init_logger, ImplRequest

Expand Down
1 change: 1 addition & 0 deletions tests/templates/wallpaper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is formatted with Python Black
# mypy: disable-error-code="misc"

from tests.templates import Response, init_logger, ImplRequest

Expand Down
68 changes: 16 additions & 52 deletions tests/test_document_fuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import sys
import multiprocessing as mp
import traceback
from collections import defaultdict
from gi.repository import Gio, GLib


Expand All @@ -28,7 +29,7 @@ def filename_to_ay(filename):
dir_prefix = "dir"
ensure_no_remaining = True

running_count = {}
running_count: defaultdict[str, int] = defaultdict(int)


def log(str):
Expand All @@ -39,15 +40,10 @@ def logv(str):
log(str)


def get_a_count(counter):
def get_a_count(counter: str):
global running_count
if counter in running_count:
count = running_count[counter]
count = count + 1
running_count[counter] = count
return count
running_count[counter] = 1
return 1
running_count[counter] += 1
return running_count[counter]


def setFileContent(path, content):
Expand Down Expand Up @@ -83,67 +79,35 @@ def appendFdContent(fd, content):


def assertRaises(exc_type, func, *args, **kwargs):
raised_exc = None
try:
with pytest.raises(exc_type):
func(*args, **kwargs)
except Exception:
raised_exc = sys.exc_info()[0]

if not raised_exc:
raise AssertionError("{0} was not raised".format(exc_type.__name__))
if raised_exc != exc_type:
raise AssertionError(
"Wrong assertion type {0} was raised instead of {1}".format(
raised_exc.__name__, exc_type.__name__
)
)


def assertRaisesErrno(error_nr, func, *args, **kwargs):
raised_exc = None
raised_exc_value = None
try:
with pytest.raises(OSError) as excinfo:
func(*args, **kwargs)
except Exception:
raised_exc = sys.exc_info()[0]
raised_exc_value = sys.exc_info()[1]

if not raised_exc:
raise AssertionError("No assertion was raised")
if isinstance(raised_exc, OSError):
raise AssertionError("OSError was not raised")
if raised_exc_value.errno != error_nr:

if excinfo.value.errno != error_nr:
raise AssertionError(
"Wrong errno {0} was raised instead of {1}".format(
raised_exc_value.errno, error_nr
excinfo.value.errno, error_nr
)
)


def assertRaisesGError(message, code, func, *args, **kwargs):
raised_exc = None
raised_exc_value = None
try:
with pytest.raises(GLib.GError) as excinfo:
func(*args, **kwargs)
except Exception:
raised_exc = sys.exc_info()[0]
raised_exc_value = sys.exc_info()[1]

if not raised_exc:
raise AssertionError("No assertion was raised")
if raised_exc != GLib.GError:
raise AssertionError("GError was not raised")
if not raised_exc_value.message.startswith(message):

if not excinfo.value.message.startswith(message):
raise AssertionError(
"Wrong message {0} doesn't start with {1}".format(
raised_exc_value.message, message
excinfo.value.message, message
)
)
if raised_exc_value.code != code:
if excinfo.value.code != code:
raise AssertionError(
"Wrong code {0} was raised instead of {1}".format(
raised_exc_value.code, code
)
"Wrong code {0} was raised instead of {1}".format(excinfo.value.code, code)
)


Expand Down
1 change: 1 addition & 0 deletions tests/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def create_dummy_session(self, dbus_con):
"CreateSession",
options=options,
)
assert response
assert response.response == 0

return xdp.Session.from_response(dbus_con, response)
Expand Down
Loading
Loading