From a27982f8ef30e30ef10dbdff05def1f063b9e085 Mon Sep 17 00:00:00 2001 From: Russell Martin Date: Tue, 28 Nov 2023 13:53:13 -0500 Subject: [PATCH 1/3] Clarify imports that are dependent on the version of Python --- .pre-commit-config.yaml | 12 ++++++------ changes/2249.misc.rst | 1 + core/pyproject.toml | 1 + core/src/toga/__init__.py | 4 ++-- core/src/toga/platform.py | 13 +++++-------- core/tests/test_platform.py | 13 +++++-------- web/src/toga_web/libs.py | 10 ++++------ 7 files changed, 24 insertions(+), 30 deletions(-) create mode 100644 changes/2249.misc.rst diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index cc66b63004..123fa3faa9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,16 +9,15 @@ repos: - id: check-docstring-first - id: end-of-file-fixer - id: trailing-whitespace - - repo: https://github.com/PyCQA/isort - rev: 5.12.0 - hooks: - - id: isort - additional_dependencies: [toml] - repo: https://github.com/asottile/pyupgrade rev: v3.15.0 hooks: - id: pyupgrade args: [--py38-plus] + - repo: https://github.com/PyCQA/isort + rev: 5.12.0 + hooks: + - id: isort - repo: https://github.com/psf/black-pre-commit-mirror rev: 23.11.0 hooks: @@ -32,4 +31,5 @@ repos: rev: v2.2.6 hooks: - id: codespell - additional_dependencies: [tomli] + # remove toml extra once Python 3.10 is no longer supported + additional_dependencies: ['.[toml]'] diff --git a/changes/2249.misc.rst b/changes/2249.misc.rst new file mode 100644 index 0000000000..23e2f063b0 --- /dev/null +++ b/changes/2249.misc.rst @@ -0,0 +1 @@ +Module imports that are dependent on the version of Python are now explicitly structured to declare the Python versions they are dependent on. diff --git a/core/pyproject.toml b/core/pyproject.toml index 11943430b5..08fc115012 100644 --- a/core/pyproject.toml +++ b/core/pyproject.toml @@ -57,6 +57,7 @@ classifiers = [ ] dependencies = [ "travertino >= 0.3.0", + # limited to <=3.9 for the `group` argument for `entry_points()` "importlib_metadata >= 4.4.0; python_version <= '3.9'", ] diff --git a/core/src/toga/__init__.py b/core/src/toga/__init__.py index 705ee52e06..0b04af1b63 100644 --- a/core/src/toga/__init__.py +++ b/core/src/toga/__init__.py @@ -105,11 +105,11 @@ def _package_version(file, name): # If it *is* in the environment, but the code isn't a git checkout (e.g., # it's been pip installed non-editable) the call to get_version() will fail. # If either of these occurs, read version from the installer metadata. - from importlib import metadata as importlib_metadata + import importlib.metadata # The Toga package names as defined in setup.cfg all use dashes. package = "toga-core" if name == "toga" else name.replace("_", "-") - return importlib_metadata.version(package) + return importlib.metadata.version(package) __version__ = _package_version(__file__, __name__) diff --git a/core/src/toga/platform.py b/core/src/toga/platform.py index c867a8cbb0..7fb438ad62 100644 --- a/core/src/toga/platform.py +++ b/core/src/toga/platform.py @@ -3,15 +3,12 @@ import sys from functools import lru_cache -try: - # Usually, the pattern is "import module; if it doesn't exist, - # import the shim". However, we need the 3.10 API for entry_points, - # as the 3.8 didn't support the `groups` argument to entry_points. - # Therefore, we try to import the compatibility shim first; and fall - # back to the stdlib module if the shim isn't there. - from importlib_metadata import entry_points -except ImportError: +if sys.version_info >= (3, 10): from importlib.metadata import entry_points +else: + # Before Python 3.10, entry_points did not support the group argument; + # so, the backport package must be used on older versions. + from importlib_metadata import entry_points # Map python sys.platform with toga platforms names diff --git a/core/tests/test_platform.py b/core/tests/test_platform.py index 4d145e27d4..0e428861cb 100644 --- a/core/tests/test_platform.py +++ b/core/tests/test_platform.py @@ -5,15 +5,12 @@ import toga_dummy -try: - # Usually, the pattern is "import module; if it doesn't exist, - # import the shim". However, we need the 3.10 API for entry_points, - # as the 3.8 didn't support the `groups` argument to entry_points. - # Therefore, we try to import the compatibility shim first; and fall - # back to the stdlib module if the shim isn't there. - from importlib_metadata import EntryPoint -except ImportError: +if sys.version_info >= (3, 10): from importlib.metadata import EntryPoint +else: + # Before Python 3.10, entry_points did not support the group argument; + # so, the backport package must be used on older versions. + from importlib_metadata import EntryPoint import toga.platform from toga.platform import current_platform, get_current_platform, get_platform_factory diff --git a/web/src/toga_web/libs.py b/web/src/toga_web/libs.py index 68d53ecc0b..669230a680 100644 --- a/web/src/toga_web/libs.py +++ b/web/src/toga_web/libs.py @@ -1,18 +1,16 @@ try: - # Try to import js from the PyScript namespace. + # Try to import js from the PyScript namespace import js except ModuleNotFoundError: - # To ensure the code can be imported, provide a js symbol - # as a fallback + # To ensure the code can be imported, provide a js symbol as a fallback js = None try: - # Try to import pyodide from the PyScript namespace. + # Try to import pyodide from the PyScript namespace import pyodide except ModuleNotFoundError: - # To ensure the code can be imported, provide a js symbol - # as a fallback + # To ensure the code can be imported, provide a pyodide symbol as a fallback pyodide = None From 9a876f36219b4d90cd8f84027af6596174c82558 Mon Sep 17 00:00:00 2001 From: Russell Martin Date: Tue, 28 Nov 2023 14:32:53 -0500 Subject: [PATCH 2/3] Run testbed CI with Briefcase before the emulator skin was changed --- .github/workflows/ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 297c2b5341..a9d8180511 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -222,7 +222,8 @@ jobs: ${{ matrix.pre-command }} # Use the development version of Briefcase python -m pip install -U pip - python -m pip install git+https://github.com/beeware/briefcase.git + # python -m pip install git+https://github.com/beeware/briefcase.git + python -m pip install git+https://github.com/beeware/briefcase.git@018931b3c6428896edb7ebffb76680602ad5d06b - name: Test App working-directory: testbed From 49f44cd1b3bf022dacf0f9fd43fabaae21b9ba72 Mon Sep 17 00:00:00 2001 From: Russell Martin Date: Tue, 28 Nov 2023 14:45:06 -0500 Subject: [PATCH 3/3] Run testbed on Android emulator with pixel_3a skin --- .github/workflows/ci.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a9d8180511..68fde2d7e3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -193,7 +193,7 @@ jobs: - backend: android runs-on: ubuntu-latest - briefcase-run-args: " -d '{\"avd\":\"beePhone\"}' --Xemulator=-no-window --Xemulator=-no-snapshot --Xemulator=-no-audio --Xemulator=-no-boot-anim --shutdown-on-exit" + briefcase-run-args: " -d '{\"avd\":\"beePhone\",\"skin\":\"pixel_3a\"}' --Xemulator=-no-window --Xemulator=-no-snapshot --Xemulator=-no-audio --Xemulator=-no-boot-anim --shutdown-on-exit" pre-command: | # check if virtualization is supported... sudo apt install -qq --no-install-recommends cpu-checker coreutils && echo "CPUs=$(nproc --all)" && kvm-ok @@ -222,8 +222,7 @@ jobs: ${{ matrix.pre-command }} # Use the development version of Briefcase python -m pip install -U pip - # python -m pip install git+https://github.com/beeware/briefcase.git - python -m pip install git+https://github.com/beeware/briefcase.git@018931b3c6428896edb7ebffb76680602ad5d06b + python -m pip install git+https://github.com/beeware/briefcase.git - name: Test App working-directory: testbed