Skip to content

Commit 5cff3a0

Browse files
hroncokparthea
andauthored
fix: Add support for Python 3.11 (#329)
* Adjust to enum changes in Python 3.11.0b3 There are two changes: Changes in the actual code: - _member_names changed from a list to a dict in python/cpython#28907 - we instance-check and remove by list-specific or dict-specific way Change in the tests only: - accessing other enum members via instance attributes is no longer possible - we access them via the class instead - we leave the original test in a try-except block Some of the Python enum changes might get reverted, see python/cpython#93910 But the fix is backwards compatible. Fixes #326 * ci: unit test session with python 3.11.0-beta.3 * ci: add python v3.11.0-beta.3 to noxfile.py * another attempt to get python 3.11.0b3 working in github actions * ci: use python 3.8 for docs check * ci: fix docs build * fix ci * mark python 3.11 tests as required * add python 3.11 to setup.py * fix docs build * remove python 3.11 test for unitcpp * remove python 3.11 test for unitcpp * remove python 3.11 test for unitcpp * attempt to fix exclude in github action Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent a66a378 commit 5cff3a0

File tree

7 files changed

+35
-14
lines changed

7 files changed

+35
-14
lines changed

.github/sync-repo-settings.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ branchProtectionRules:
1616
- 'unit (3.9)'
1717
- 'unit (3.10, cpp)'
1818
- 'unit (3.10)'
19+
- 'unit (3.11)'
1920
- cover
2021
- OwlBot Post Processor
2122
- 'cla/google'

.github/workflows/tests.yml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,23 @@ jobs:
3030
runs-on: ubuntu-latest
3131
steps:
3232
- uses: actions/checkout@v2
33-
- name: Set up Python 3.8
33+
- name: Set up Python 3.9
3434
uses: actions/setup-python@v4
3535
with:
36-
python-version: "3.10"
36+
python-version: "3.9"
3737
- name: Install nox.
3838
run: python -m pip install nox
3939
- name: Build the documentation.
4040
run: nox -s docs
4141
unit:
42-
runs-on: ubuntu-latest
42+
runs-on: ubuntu-20.04
4343
strategy:
4444
matrix:
45-
python: ['3.6', '3.7', '3.8', '3.9', '3.10']
45+
python: ['3.6', '3.7', '3.8', '3.9', '3.10', '3.11']
4646
variant: ['', 'cpp', 'upb']
47+
exclude:
48+
- variant: "cpp"
49+
python: 3.11
4750
steps:
4851
- uses: actions/checkout@v2
4952
- name: Set up Python ${{ matrix.python }}
@@ -53,16 +56,22 @@ jobs:
5356
- name: Install nox
5457
run: |
5558
pip install nox
59+
# Trim the Python version string
60+
- name: Trim python version
61+
run: |
62+
PYTHON_VERSION_TRIMMED=${{ matrix.python }}
63+
PYTHON_VERSION_TRIMMED=$(echo $PYTHON_VERSION_TRIMMED | cut -c1-4)
64+
echo "PYTHON_VERSION_TRIMMED=$PYTHON_VERSION_TRIMMED" >> $GITHUB_ENV
5665
- name: Run unit tests
5766
env:
58-
COVERAGE_FILE: .coverage-${{ matrix.variant }}-${{ matrix.python }}
67+
COVERAGE_FILE: .coverage-${{ matrix.variant }}-${{ env.PYTHON_VERSION_TRIMMED }}
5968
run: |
60-
nox -s unit${{ matrix.variant }}-${{ matrix.python }}
69+
nox -s unit${{ matrix.variant }}-${{ env.PYTHON_VERSION_TRIMMED }}
6170
- name: Upload coverage results
6271
uses: actions/upload-artifact@v3
6372
with:
6473
name: coverage-artifacts
65-
path: .coverage-${{ matrix.variant }}-${{ matrix.python }}
74+
path: .coverage-${{ matrix.variant }}-${{ env.PYTHON_VERSION_TRIMMED }}
6675
cover:
6776
runs-on: ubuntu-latest
6877
needs:

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
#
6565
# This is also used if you do content translation via gettext catalogs.
6666
# Usually you set "language" from the command line for these cases.
67-
language = None
67+
language = "en"
6868

6969
# List of patterns, relative to source directory, that match files and
7070
# directories to ignore when looking for source files.

noxfile.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@
2828
"3.8",
2929
"3.9",
3030
"3.10",
31+
"3.11",
3132
]
3233

34+
# Error if a python version is missing
35+
nox.options.error_on_missing_interpreters = True
36+
3337

3438
@nox.session(python=PYTHON_VERSIONS)
3539
def unit(session, proto="python"):
@@ -64,7 +68,7 @@ def unit(session, proto="python"):
6468
# Check if protobuf has released wheels for new python versions
6569
# https://pypi.org/project/protobuf/#files
6670
# This list will generally be shorter than 'unit'
67-
@nox.session(python=PYTHON_VERSIONS)
71+
@nox.session(python=["3.6", "3.7", "3.8", "3.9", "3.10"])
6872
def unitcpp(session):
6973
return unit(session, proto="cpp")
7074

@@ -74,8 +78,7 @@ def unitupb(session):
7478
return unit(session, proto="upb")
7579

7680

77-
# Just use the most recent version for docs
78-
@nox.session(python=PYTHON_VERSIONS[-1])
81+
@nox.session(python="3.9")
7982
def docs(session):
8083
"""Build the docs."""
8184

proto/enums.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ def __new__(mcls, name, bases, attrs):
5858
# In 3.7 onwards, we can define an _ignore_ attribute and do some
5959
# mucking around with that.
6060
if pb_options in attrs._member_names:
61-
idx = attrs._member_names.index(pb_options)
62-
attrs._member_names.pop(idx)
61+
if isinstance(attrs._member_names, list):
62+
idx = attrs._member_names.index(pb_options)
63+
attrs._member_names.pop(idx)
64+
else: # Python 3.11.0b3
65+
del attrs._member_names[pb_options]
6366

6467
# Make the descriptor.
6568
enum_desc = descriptor_pb2.EnumDescriptorProto(

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"Programming Language :: Python :: 3.8",
5656
"Programming Language :: Python :: 3.9",
5757
"Programming Language :: Python :: 3.10",
58+
"Programming Language :: Python :: 3.11",
5859
"Topic :: Software Development :: Code Generators",
5960
"Topic :: Software Development :: Libraries :: Python Modules",
6061
],

tests/test_enum_total_ordering.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ def test_total_ordering_w_other_enum_type():
4949

5050
for item in enums_test.OtherEnum:
5151
assert not to_compare == item
52-
assert to_compare.SOME_VALUE != item
52+
assert type(to_compare).SOME_VALUE != item
53+
try:
54+
assert to_compare.SOME_VALUE != item
55+
except AttributeError: # Python 3.11.0b3
56+
pass
5357
with pytest.raises(TypeError):
5458
assert not to_compare < item
5559
with pytest.raises(TypeError):

0 commit comments

Comments
 (0)