From 6344840a461c07dbe782c74f5c58ec8df7b4a5e0 Mon Sep 17 00:00:00 2001 From: Greg Lucas Date: Mon, 6 Mar 2023 07:11:32 -0700 Subject: [PATCH] chore(python-runtime): Update importlib resources to handle Python 3.11 deprecations (#3986) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Co-authored-by: Romain Marcadier --- .all-contributorsrc | 9 ++++++ .github/workflows/main.yml | 7 +++++ .mergify/config.yml | 4 +++ README.md | 29 ++++++++++--------- .../configuration/targets/python.md | 1 + packages/@jsii/python-runtime/pyproject.toml | 2 +- packages/@jsii/python-runtime/setup.py | 2 ++ .../@jsii/python-runtime/src/jsii/__meta__.py | 4 ++- .../@jsii/python-runtime/src/jsii/_compat.py | 5 +--- .../src/jsii/_kernel/providers/process.py | 6 +++- .../@jsii/python-runtime/src/jsii/_runtime.py | 8 +++-- packages/jsii-pacmak/lib/targets/python.ts | 1 + .../__snapshots__/examples.test.js.snap | 2 ++ .../prerelease-identifiers.test.js.snap | 4 +++ .../__snapshots__/target-python.test.js.snap | 4 +++ 15 files changed, 64 insertions(+), 24 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 4dd91a9bf3..0ed1c67de7 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -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", diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 869dc2d66d..cae45cbe5a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 }} diff --git a/.mergify/config.yml b/.mergify/config.yml index be5e069cdc..e4ac664195 100644 --- a/.mergify/config.yml +++ b/.mergify/config.yml @@ -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 @@ -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: @@ -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: @@ -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: diff --git a/README.md b/README.md index a451c1a826..87a3a7a96f 100644 --- a/README.md +++ b/README.md @@ -129,131 +129,132 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d Graham Lea
Graham Lea

🤔 👀 + Greg Lucas
Greg Lucas

💻 Hamza Assyad
Hamza Assyad

🐛 💻 🤔 👀 Hari Pachuveetil
Hari Pachuveetil

📝 📖 Hsing-Hui Hsu
Hsing-Hui Hsu

💻 📖 🤔 👀 Ikko Ashimine
Ikko Ashimine

📖 James
James

🐛 💻 - James Kelley
James Kelley

🐛 + James Kelley
James Kelley

🐛 James Mead
James Mead

💻 James Siri
James Siri

💻 🚧 Jason Del Ponte
Jason Del Ponte

🤔 👀 Jason Fulghum
Jason Fulghum

🤔 📆 👀 Jeff Malins
Jeff Malins

💻 Jerry Kindall
Jerry Kindall

📖 🤔 - Jimmy Gaussen
Jimmy Gaussen

🤔 + Jimmy Gaussen
Jimmy Gaussen

🤔 Johannes Weber
Johannes Weber

📖 John Pantzlaff
John Pantzlaff

💻 Jon Steinich
Jon Steinich

🐛 🤔 💻 Joseph Lawson
Joseph Lawson

👀 Joseph Martin
Joseph Martin

🐛 Junix
Junix

🐛 - Justin Frahm
Justin Frahm

🐛 + Justin Frahm
Justin Frahm

🐛 Justin Taylor
Justin Taylor

🐛 Kaizen Conroy
Kaizen Conroy

💻 🐛 Kaizen Conroy
Kaizen Conroy

💻 Kaushik Borra
Kaushik Borra

🐛 Knut O. Hellan
Knut O. Hellan

🐛 Kyle Thomson
Kyle Thomson

💻 👀 - Leandro Padua
Leandro Padua

🐛 + Leandro Padua
Leandro Padua

🐛 Liang Zhou
Liang Zhou

🐛 💻 Madeline Kusters
Madeline Kusters

💻 🐛 Maja S Bratseth
Maja S Bratseth

🐛 Marcos Diez
Marcos Diez

🐛 Mark Nielsen
Mark Nielsen

💻 Matthew Bonig
Matthew Bonig

🐛 📝 - Matthew Pirocchi
Matthew Pirocchi

💻 🤔 👀 + Matthew Pirocchi
Matthew Pirocchi

💻 🤔 👀 Meng Xin Zhu
Meng Xin Zhu

🐛 Michael Neil
Michael Neil

🚧 Mike Lane
Mike Lane

🐛 Mitch Garnaat
Mitch Garnaat

🐛 💻 🤔 👀 Mitchell Valine
Mitchell Valine

🐛 💻 🤔 🚧 👀 Mohamad Soufan
Mohamad Soufan

📖 - Mykola Mogylenko
Mykola Mogylenko

🐛 + Mykola Mogylenko
Mykola Mogylenko

🐛 Naumel
Naumel

👀 Neta Nir
Neta Nir

💻 🤔 🚧 👀 Nick Lynch
Nick Lynch

🐛 💻 🚧 👀 Niranjan Jayakar
Niranjan Jayakar

🐛 💻 🤔 🚧 👀 Noah Litov
Noah Litov

💻 🚧 👀 Otavio Macedo
Otavio Macedo

💻 🐛 - PIDZ - Bart
PIDZ - Bart

🤔 + PIDZ - Bart
PIDZ - Bart

🤔 Peter Woodworth
Peter Woodworth

🚧 Petr Kacer
Petr Kacer

🐛 Petra Barus
Petra Barus

💻 Philip Cali
Philip Cali

🤔 Quentin Loos
Quentin Loos

🤔 Raphael
Raphael

🐛 - Richard H Boyd
Richard H Boyd

🐛 + Richard H Boyd
Richard H Boyd

🐛 Rico Huijbers
Rico Huijbers

🐛 💻 🤔 🚧 👀 Romain Marcadier
Romain Marcadier

🐛 💻 🎨 🤔 🚧 👀 📝 SADIK KUZU
SADIK KUZU

👀 SK
SK

🤔 Sam Fink
Sam Fink

💻 👀 Sam Goodwin
Sam Goodwin

👀 - Sebastian Korfmann
Sebastian Korfmann

🐛 💻 🤔 + Sebastian Korfmann
Sebastian Korfmann

🐛 💻 🤔 Sepehr Laal
Sepehr Laal

🐛 Shane Witbeck
Shane Witbeck

🤔 Shiv Lakshminarayan
Shiv Lakshminarayan

💻 🚧 👀 Somaya
Somaya

💻 🤔 🚧 👀 Stephen Kuenzli
Stephen Kuenzli

📖 Takahiro Sugiura
Takahiro Sugiura

📖 - The Gitter Badger
The Gitter Badger

💻 🚧 + The Gitter Badger
The Gitter Badger

💻 🚧 Thomas Poignant
Thomas Poignant

🐛 Thomas Steinbach
Thomas Steinbach

🐛 Thorsten Hoeger
Thorsten Hoeger

💻 Tim Wagner
Tim Wagner

🐛 🤔 Tobias Lidskog
Tobias Lidskog

💻 Tom Bonner
Tom Bonner

🐛 - Ty Coghlan
Ty Coghlan

🐛 + Ty Coghlan
Ty Coghlan

🐛 Tyler van Hensbergen
Tyler van Hensbergen

🤔 Vlad Hrybok
Vlad Hrybok

🐛 Vladimir Shchur
Vladimir Shchur

🐛 Will Bender
Will Bender

🐛 Yan Zhulanow
Yan Zhulanow

💻 Yigong Liu
Yigong Liu

🐛 🤔 - Zach Bienenfeld
Zach Bienenfeld

🐛 + Zach Bienenfeld
Zach Bienenfeld

🐛 ajnarang
ajnarang

🤔 aniljava
aniljava

💻 arnogeurts-sqills
arnogeurts-sqills

🐛 💻 cn-cit
cn-cit

🐛 deccy-mcc
deccy-mcc

🐛 dependabot-preview[bot]
dependabot-preview[bot]

🐛 🚧 - dependabot[bot]
dependabot[bot]

🚧 + dependabot[bot]
dependabot[bot]

🚧 dheffx
dheffx

🐛 gregswdl
gregswdl

🐛 guyroberts21
guyroberts21

📖 mattBrzezinski
mattBrzezinski

📖 mergify
mergify

🚧 mergify[bot]
mergify[bot]

🚧 - nathannaveen
nathannaveen

🚧 + nathannaveen
nathannaveen

🚧 seiyashima42
seiyashima42

🐛 💻 📖 sullis
sullis

💻 vaneek
vaneek

🐛 diff --git a/gh-pages/content/user-guides/lib-author/configuration/targets/python.md b/gh-pages/content/user-guides/lib-author/configuration/targets/python.md index ab5eb8098a..7a41219615 100644 --- a/gh-pages/content/user-guides/lib-author/configuration/targets/python.md +++ b/gh-pages/content/user-guides/lib-author/configuration/targets/python.md @@ -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: diff --git a/packages/@jsii/python-runtime/pyproject.toml b/packages/@jsii/python-runtime/pyproject.toml index bc3c3c58b7..4ccebe88f3 100644 --- a/packages/@jsii/python-runtime/pyproject.toml +++ b/packages/@jsii/python-runtime/pyproject.toml @@ -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)' diff --git a/packages/@jsii/python-runtime/setup.py b/packages/@jsii/python-runtime/setup.py index a3ef3a3c1f..33e2cb80ae 100644 --- a/packages/@jsii/python-runtime/setup.py +++ b/packages/@jsii/python-runtime/setup.py @@ -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", @@ -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", diff --git a/packages/@jsii/python-runtime/src/jsii/__meta__.py b/packages/@jsii/python-runtime/src/jsii/__meta__.py index f4a54a5056..a24112ea87 100644 --- a/packages/@jsii/python-runtime/src/jsii/__meta__.py +++ b/packages/@jsii/python-runtime/src/jsii/__meta__.py @@ -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"] diff --git a/packages/@jsii/python-runtime/src/jsii/_compat.py b/packages/@jsii/python-runtime/src/jsii/_compat.py index 27798ed192..3c0830517b 100644 --- a/packages/@jsii/python-runtime/src/jsii/_compat.py +++ b/packages/@jsii/python-runtime/src/jsii/_compat.py @@ -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"] diff --git a/packages/@jsii/python-runtime/src/jsii/_kernel/providers/process.py b/packages/@jsii/python-runtime/src/jsii/_kernel/providers/process.py index 7ae62a11d5..61e5fae38a 100644 --- a/packages/@jsii/python-runtime/src/jsii/_kernel/providers/process.py +++ b/packages/@jsii/python-runtime/src/jsii/_kernel/providers/process.py @@ -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] diff --git a/packages/@jsii/python-runtime/src/jsii/_runtime.py b/packages/@jsii/python-runtime/src/jsii/_runtime.py index 19dcc31ba0..b8fd0e6c23 100644 --- a/packages/@jsii/python-runtime/src/jsii/_runtime.py +++ b/packages/@jsii/python-runtime/src/jsii/_runtime.py @@ -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)) diff --git a/packages/jsii-pacmak/lib/targets/python.ts b/packages/jsii-pacmak/lib/targets/python.ts index 831f0e41e0..0a7a010423 100644 --- a/packages/jsii-pacmak/lib/targets/python.ts +++ b/packages/jsii-pacmak/lib/targets/python.ts @@ -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, diff --git a/packages/jsii-pacmak/test/generated-code/__snapshots__/examples.test.js.snap b/packages/jsii-pacmak/test/generated-code/__snapshots__/examples.test.js.snap index 2daa7b47c7..b1c57b1df4 100644 --- a/packages/jsii-pacmak/test/generated-code/__snapshots__/examples.test.js.snap +++ b/packages/jsii-pacmak/test/generated-code/__snapshots__/examples.test.js.snap @@ -1282,6 +1282,7 @@ kwargs = json.loads( "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", "Typing :: Typed", "License :: OSI Approved" ], @@ -2704,6 +2705,7 @@ kwargs = json.loads( "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", "Typing :: Typed", "License :: OSI Approved" ], diff --git a/packages/jsii-pacmak/test/generated-code/__snapshots__/prerelease-identifiers.test.js.snap b/packages/jsii-pacmak/test/generated-code/__snapshots__/prerelease-identifiers.test.js.snap index 7e5af6ef15..fb35301af4 100644 --- a/packages/jsii-pacmak/test/generated-code/__snapshots__/prerelease-identifiers.test.js.snap +++ b/packages/jsii-pacmak/test/generated-code/__snapshots__/prerelease-identifiers.test.js.snap @@ -466,6 +466,7 @@ kwargs = json.loads( "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", "Typing :: Typed" ], "scripts": [] @@ -976,6 +977,7 @@ kwargs = json.loads( "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", "Typing :: Typed" ], "scripts": [] @@ -1465,6 +1467,7 @@ kwargs = json.loads( "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", "Typing :: Typed" ], "scripts": [] @@ -1952,6 +1955,7 @@ kwargs = json.loads( "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", "Typing :: Typed" ], "scripts": [] diff --git a/packages/jsii-pacmak/test/generated-code/__snapshots__/target-python.test.js.snap b/packages/jsii-pacmak/test/generated-code/__snapshots__/target-python.test.js.snap index 1e09f2e78f..5bc1df2363 100644 --- a/packages/jsii-pacmak/test/generated-code/__snapshots__/target-python.test.js.snap +++ b/packages/jsii-pacmak/test/generated-code/__snapshots__/target-python.test.js.snap @@ -305,6 +305,7 @@ kwargs = json.loads( "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", "Typing :: Typed", "License :: OSI Approved" ], @@ -858,6 +859,7 @@ kwargs = json.loads( "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", "Typing :: Typed", "License :: OSI Approved" ], @@ -1389,6 +1391,7 @@ kwargs = json.loads( "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", "Typing :: Typed", "Development Status :: 7 - Inactive", "License :: OSI Approved" @@ -3016,6 +3019,7 @@ kwargs = json.loads( "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", "Typing :: Typed", "Development Status :: 5 - Production/Stable", "License :: OSI Approved",