Skip to content

Commit

Permalink
Merge pull request #2249 from rmartin16/version-dependent-imports
Browse files Browse the repository at this point in the history
Clarify imports that are dependent on the version of Python
  • Loading branch information
freakboy3742 committed Nov 28, 2023
2 parents dd7f53f + 49f44cd commit 2db1d59
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]']
1 change: 1 addition & 0 deletions changes/2249.misc.rst
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
]

Expand Down
4 changes: 2 additions & 2 deletions core/src/toga/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
13 changes: 5 additions & 8 deletions core/src/toga/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions core/tests/test_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions web/src/toga_web/libs.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down

0 comments on commit 2db1d59

Please sign in to comment.