Skip to content

Commit

Permalink
chore(python-runtime): Update importlib resources to handle Python 3.…
Browse files Browse the repository at this point in the history
…11 deprecations (#3986)

Python 3.11 deprecated some usage importlib.resources and has a new back-compatibility shim through 3.9. This updates the code to the new files()-based API usage and uses the shims for versions less than 3.9.

docs.python.org/3/library/importlib.resources.html#deprecated-functions
importlib-resources.readthedocs.io/en/latest/using.html#migrating-from-legacy
discuss.python.org/t/deprecating-importlib-resources-legacy-api/11386

closes #3958

Co-authored-by: 🧑🏻‍💻 Romain Marcadier <rmuller@amazon.com>
Co-authored-by: Romain Marcadier <rmuller@amazon.fr>
  • Loading branch information
3 people authored Mar 6, 2023
1 parent 461446b commit 6344840
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 24 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -1549,6 +1549,15 @@
"contributions": [
"bug"
]
},
{
"login": "greglucas",
"name": "Greg Lucas",
"avatar_url": "https://avatars.githubusercontent.com/u/12417828?v=4",
"profile": "https://github.com/greglucas",
"contributions": [
"code"
]
}
],
"repoType": "github",
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,13 @@ jobs:
java: '8'
node: '14'
os: ubuntu-latest
- title: 'Python 3.11'
python: '3.11'
dotnet: '6.0.x'
go: '1.18'
java: '8'
node: '14'
os: ubuntu-latest

runs-on: ${{ matrix.os }}

Expand Down
4 changes: 4 additions & 0 deletions .mergify/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ queue_rules:
- status-success=Test (Python 3.8)
- status-success=Test (Python 3.9)
- status-success=Test (Python 3.10)
- status-success=Test (Python 3.11)

pull_request_rules:
- name: label core
Expand Down Expand Up @@ -71,6 +72,7 @@ pull_request_rules:
- status-success=Test (Python 3.8)
- status-success=Test (Python 3.9)
- status-success=Test (Python 3.10)
- status-success=Test (Python 3.11)

- name: Synchronize that PR to upstream and merge it (squash)
actions:
Expand Down Expand Up @@ -120,6 +122,7 @@ pull_request_rules:
- status-success=Test (Python 3.8)
- status-success=Test (Python 3.9)
- status-success=Test (Python 3.10)
- status-success=Test (Python 3.11)

- name: Synchronize that PR to upstream and merge it (no-squash)
actions:
Expand Down Expand Up @@ -169,6 +172,7 @@ pull_request_rules:
- status-success=Test (Python 3.8)
- status-success=Test (Python 3.9)
- status-success=Test (Python 3.10)
- status-success=Test (Python 3.11)

- name: Clean branch up
actions:
Expand Down
29 changes: 15 additions & 14 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The `python` target requires two configuration entries:
- `Programming Language :: Python :: 3.8`
- `Programming Language :: Python :: 3.9`
- `Programming Language :: Python :: 3.10`
- `Programming Language :: Python :: 3.11`

Example:

Expand Down
2 changes: 1 addition & 1 deletion packages/@jsii/python-runtime/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["setuptools~=62.2", "wheel~=0.37"]
build-backend = 'setuptools.build_meta'

[tool.black]
target-version = ['py37', 'py38', 'py39', 'py310']
target-version = ['py37', 'py38', 'py39', 'py310', 'py311']
include = '\.pyi?$'
exclude = '\.(git|mypy_cache|env)'

Expand Down
2 changes: 2 additions & 0 deletions packages/@jsii/python-runtime/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
install_requires=[
"attrs>=21.2,<23.0",
"cattrs>=1.8,<22.3",
"importlib_resources>=5.2.0",
"publication>=0.0.3", # This is used by all generated code.
"typeguard~=2.13.3", # This is used by all generated code.
"python-dateutil",
Expand All @@ -48,6 +49,7 @@
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Topic :: Software Development :: Libraries",
"Topic :: Utilities",
"Typing :: Typed",
Expand Down
4 changes: 3 additions & 1 deletion packages/@jsii/python-runtime/src/jsii/__meta__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from ._compat import importlib_resources

# Load our version number and other metadata.
_meta = json.loads(importlib_resources.read_text("jsii", "_metadata.json"))
_meta = json.loads(
importlib_resources.files("jsii").joinpath("_metadata.json").read_text()
)

__version__ = _meta["version"]
__jsii_runtime_version__ = _meta["jsii-runtime"]["version"]
Expand Down
5 changes: 1 addition & 4 deletions packages/@jsii/python-runtime/src/jsii/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
import sys


if sys.version_info >= (3, 7):
import importlib.resources as importlib_resources
else:
import importlib_resources
import importlib_resources


__all__ = ["importlib_resources"]
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@ def _jsii_runtime(self) -> str:
for resname, filename in resources.items():
pathlib.Path(os.path.dirname(filename)).mkdir(exist_ok=True)
with open(filename, "wb") as fp:
fp.write(importlib_resources.read_binary(jsii._embedded.jsii, resname))
fp.write(
importlib_resources.files(jsii._embedded.jsii)
.joinpath(resname)
.read_bytes()
)

# Return our first path, which should be the path for jsii-runtime.js
return resources[jsii._embedded.jsii.ENTRYPOINT]
Expand Down
8 changes: 5 additions & 3 deletions packages/@jsii/python-runtime/src/jsii/_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ def load(cls, *args, _kernel=kernel, **kwargs) -> "JSIIAssembly":
assembly = cls(*args, **kwargs)

# Actually load the assembly into the kernel, we're using the
# importlib.resources API here isntead of manually constructing the path, in
# importlib.resources API here instead of manually constructing the path, in
# the hopes that this will make JSII modules able to be used with zipimport
# instead of only on the FS.
with importlib_resources.path(
f"{assembly.module}._jsii", assembly.filename
with importlib_resources.as_file(
importlib_resources.files(f"{assembly.module}._jsii").joinpath(
assembly.filename
)
) as assembly_path:
_kernel.load(assembly.name, assembly.version, os.fspath(assembly_path))

Expand Down
1 change: 1 addition & 0 deletions packages/jsii-pacmak/lib/targets/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2124,6 +2124,7 @@ class Package {
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Typing :: Typed',
],
scripts,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6344840

Please sign in to comment.