Skip to content

Commit 69aa17f

Browse files
[MISC] Fix Python 3.12 compatibility (#428)
This PR fixes Python 3.12 compatibility after the pathlib.Path family of classes changed in order to tweak how they behave when they are are extended via sub-classes. #### References - Python 3.12 [release notes](https://docs.python.org/3/whatsnew/3.12.html#pathlib). #### Additional changes - Update `data-integrator` charm base to fix integration tests
1 parent 1c40cd6 commit 69aa17f

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939

4040
unit-test:
4141
name: Unit test charm
42-
runs-on: ubuntu-22.04 # TODO: use ubuntu-latest after fixing pathlib issue on python 3.12
42+
runs-on: ubuntu-latest
4343
timeout-minutes: 20
4444
steps:
4545
- name: Checkout

src/rock.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import ops
1010

1111
import container
12+
import utils
1213

1314
if typing.TYPE_CHECKING:
1415
import relations.cos
@@ -22,10 +23,14 @@
2223
class _Path(container.Path):
2324
"""Rock filesystem path"""
2425

25-
def __new__(cls, *args, container_: ops.Container):
26-
path = super().__new__(cls, *args)
27-
path._container = container_
28-
return path
26+
# TODO python3.10 min version: remove when min version >= 3.12
27+
def __new__(cls, *args, **kwargs):
28+
return super().__new__(cls, *args)
29+
30+
def __init__(self, *args, container_: ops.Container):
31+
if utils.python_version_after_3_12:
32+
super().__init__(*args)
33+
self._container = container_
2934

3035
def __truediv__(self, other):
3136
return type(self)(self, other, container_=self._container)

src/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
import secrets
77
import string
8+
import sys
9+
10+
python_version_after_3_12 = all((
11+
sys.version_info[0] == 3,
12+
sys.version_info[1] >= 12,
13+
))
814

915

1016
def generate_password() -> str:

0 commit comments

Comments
 (0)