Skip to content

Commit

Permalink
fix: Add support for Python 3.11 (#329)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
hroncok and parthea authored Jan 5, 2023
1 parent a66a378 commit 5cff3a0
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 14 deletions.
1 change: 1 addition & 0 deletions .github/sync-repo-settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ branchProtectionRules:
- 'unit (3.9)'
- 'unit (3.10, cpp)'
- 'unit (3.10)'
- 'unit (3.11)'
- cover
- OwlBot Post Processor
- 'cla/google'
Expand Down
23 changes: 16 additions & 7 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,23 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8
- name: Set up Python 3.9
uses: actions/setup-python@v4
with:
python-version: "3.10"
python-version: "3.9"
- name: Install nox.
run: python -m pip install nox
- name: Build the documentation.
run: nox -s docs
unit:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
strategy:
matrix:
python: ['3.6', '3.7', '3.8', '3.9', '3.10']
python: ['3.6', '3.7', '3.8', '3.9', '3.10', '3.11']
variant: ['', 'cpp', 'upb']
exclude:
- variant: "cpp"
python: 3.11
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python }}
Expand All @@ -53,16 +56,22 @@ jobs:
- name: Install nox
run: |
pip install nox
# Trim the Python version string
- name: Trim python version
run: |
PYTHON_VERSION_TRIMMED=${{ matrix.python }}
PYTHON_VERSION_TRIMMED=$(echo $PYTHON_VERSION_TRIMMED | cut -c1-4)
echo "PYTHON_VERSION_TRIMMED=$PYTHON_VERSION_TRIMMED" >> $GITHUB_ENV
- name: Run unit tests
env:
COVERAGE_FILE: .coverage-${{ matrix.variant }}-${{ matrix.python }}
COVERAGE_FILE: .coverage-${{ matrix.variant }}-${{ env.PYTHON_VERSION_TRIMMED }}
run: |
nox -s unit${{ matrix.variant }}-${{ matrix.python }}
nox -s unit${{ matrix.variant }}-${{ env.PYTHON_VERSION_TRIMMED }}
- name: Upload coverage results
uses: actions/upload-artifact@v3
with:
name: coverage-artifacts
path: .coverage-${{ matrix.variant }}-${{ matrix.python }}
path: .coverage-${{ matrix.variant }}-${{ env.PYTHON_VERSION_TRIMMED }}
cover:
runs-on: ubuntu-latest
needs:
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None
language = "en"

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
Expand Down
9 changes: 6 additions & 3 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@
"3.8",
"3.9",
"3.10",
"3.11",
]

# Error if a python version is missing
nox.options.error_on_missing_interpreters = True


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

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


# Just use the most recent version for docs
@nox.session(python=PYTHON_VERSIONS[-1])
@nox.session(python="3.9")
def docs(session):
"""Build the docs."""

Expand Down
7 changes: 5 additions & 2 deletions proto/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ def __new__(mcls, name, bases, attrs):
# In 3.7 onwards, we can define an _ignore_ attribute and do some
# mucking around with that.
if pb_options in attrs._member_names:
idx = attrs._member_names.index(pb_options)
attrs._member_names.pop(idx)
if isinstance(attrs._member_names, list):
idx = attrs._member_names.index(pb_options)
attrs._member_names.pop(idx)
else: # Python 3.11.0b3
del attrs._member_names[pb_options]

# Make the descriptor.
enum_desc = descriptor_pb2.EnumDescriptorProto(
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Topic :: Software Development :: Code Generators",
"Topic :: Software Development :: Libraries :: Python Modules",
],
Expand Down
6 changes: 5 additions & 1 deletion tests/test_enum_total_ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ def test_total_ordering_w_other_enum_type():

for item in enums_test.OtherEnum:
assert not to_compare == item
assert to_compare.SOME_VALUE != item
assert type(to_compare).SOME_VALUE != item
try:
assert to_compare.SOME_VALUE != item
except AttributeError: # Python 3.11.0b3
pass
with pytest.raises(TypeError):
assert not to_compare < item
with pytest.raises(TypeError):
Expand Down

0 comments on commit 5cff3a0

Please sign in to comment.