Skip to content

(cleanup) remove Python 2 remaining items#727

Draft
mykaul wants to merge 11 commits into
scylladb:masterfrom
mykaul:python_2_no_more
Draft

(cleanup) remove Python 2 remaining items#727
mykaul wants to merge 11 commits into
scylladb:masterfrom
mykaul:python_2_no_more

Conversation

@mykaul
Copy link
Copy Markdown

@mykaul mykaul commented Mar 4, 2026

Pre-review checklist

This is 100% OpenCode's work. So take it with a grain of salt, and I need to go over it. I can also cherry-pick each one separately. I've asked it to separate as much as possible to independent items.

  • I have split my patch into logically separate commits.
  • All commit messages clearly explain what they change and why.
  • I added relevant tests for new features and bug fixes.
  • All commits compile, pass static checks and pass test.
  • PR description sums up the changes and reasons why they should be introduced.
  • I have provided docstrings for the public items that I want to introduce.
  • I have adjusted the documentation in ./docs/source/.
  • I added appropriate Fixes: annotations to PR description.

@mykaul mykaul marked this pull request as draft March 4, 2026 19:57
@mykaul mykaul requested a review from Copilot March 4, 2026 19:57
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR continues the Python 2 cleanup by removing remaining unicode/UnicodeMixin compatibility shims and updating tests/docs/code paths to assume Python 3-only semantics (the project now requires Python >=3.9).

Changes:

  • Removes Python 2-era unicode patterns (u'', __unicode__, UnicodeMixin) and normalizes string handling across driver and cqlengine.
  • Simplifies Python-version conditionals/fallback imports (e.g., WeakSet imports) and applies formatting-only refactors in several modules/tests.
  • Updates unit/integration tests and Sphinx config to reflect Python 3-only behavior and representations.

Reviewed changes

Copilot reviewed 34 out of 37 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
tests/unit/test_types.py Replaces u'' literals with plain str in type read/write tests.
tests/unit/test_row_factories.py Removes Python 3.0–3.6 conditional expectations for namedtuple creation.
tests/unit/test_orderedmap.py Updates unicode-key tests to Python 3 str keys.
tests/unit/test_metadata.py Replaces u'' literals with str in metadata CQL export tests.
tests/unit/test_marshalling.py Updates UTF8/unicode expectations to Python 3 str and cleans up ordered map inserts.
tests/unit/advanced/test_insights.py Removes Python-version-specific namespace logic and reformats expected dicts.
tests/integration/standard/test_query.py Updates unicode query strings/column names to Python 3 str.
tests/integration/standard/test_cluster.py Updates expected row tuples to Python 3 str.
tests/integration/cqlengine/model/test_udts.py Updates unicode literals to Python 3 str.
tests/integration/cqlengine/model/test_model_io.py Updates unicode literals to Python 3 str in model IO assertions.
tests/integration/cqlengine/model/test_class_construction.py Mostly formatting + Python 3 iterator usage (next(iter(...))) and string literal normalization.
tests/integration/cqlengine/columns/test_validation.py Removes old Python-version branches and normalizes string usage/formatting in validation tests.
setup.py Removes Python 2-era subprocess gating and refactors extension/doc build setup logic.
docs/conf.py Normalizes string literals and formatting in Sphinx configuration.
cassandra/query.py Python 3 string/formatting cleanup; keeps namedtuple fallback paths but modernizes literals/layout.
cassandra/pool.py Removes legacy WeakSet fallback and reformats/shard-aware related code blocks.
cassandra/io/asyncorereactor.py Removes legacy WeakSet fallback and modernizes literals/formatting.
cassandra/encoder.py Deprecates Python 2 “unicode” semantics and standardizes encoding/quoting behavior for Python 3.
cassandra/datastax/graph/query.py Python 3 string/formatting cleanup and minor readability refactors.
cassandra/datastax/graph/graphson.py Python 3 cleanup + formatting; updates docs/comments describing supported Python types.
cassandra/datastax/graph/fluent/_query.py Python 3 string/formatting cleanup and improves readability of traversal query generation.
cassandra/cqlengine/statements.py Removes UnicodeMixin usage and converts __unicode__ implementations to __str__.
cassandra/cqlengine/operators.py Removes UnicodeMixin usage and converts operator stringification to __str__.
cassandra/cqlengine/named.py Converts __unicode__ to __str__ and normalizes string literals.
cassandra/cqlengine/models.py Removes UnicodeMixin usage and normalizes string literals/formatting across model machinery.
cassandra/cqlengine/functions.py Removes UnicodeMixin usage and converts __unicode__ to __str__.
cassandra/cqlengine/init.py Removes UnicodeMixin definition entirely.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread setup.py Outdated
Comment thread setup.py Outdated
Comment thread setup.py Outdated
Comment thread cassandra/datastax/graph/graphson.py Outdated
Comment thread tests/unit/test_row_factories.py
Comment thread tests/unit/test_orderedmap.py
@mykaul mykaul force-pushed the python_2_no_more branch from 172606e to 59b6813 Compare March 5, 2026 16:43
@mykaul
Copy link
Copy Markdown
Author

mykaul commented Mar 5, 2026

Fixed all comments.

@mykaul
Copy link
Copy Markdown
Author

mykaul commented Mar 5, 2026

@copilot code review[agent] - please re-review

@mykaul
Copy link
Copy Markdown
Author

mykaul commented Mar 16, 2026

Review: Rebased onto master, Python 3 expert review

Rebase Status

Rebased all 18 commits onto current master. One conflict in cassandra/cluster.py (tablets routing ctype caching vs. quote-style change) — resolved by keeping master's caching pattern with the PR's double-quote formatting. All unit tests pass.

Review Findings

The 18 commits are semantically correct. The bulk of the diff is formatting (quote normalization, import reformatting). All major semantic changes verified:

  • __unicode____str__: Complete. No remaining __unicode__, UnicodeMixin, or __nonzero__ references.
  • WeakSet removal: All 3 import sites correctly use from weakref import WeakSet. Custom implementation fully removed.
  • filter() materialization: All 3 sites correctly wrap filter() in list() where the result needs indexing or .pop().
  • u'' prefix removal: Complete.
  • from __future__ import absolute_import: Complete.
  • cql_encode_unicode fix: Old code cql_quote(val.encode('utf-8')) would produce broken results on Py3 (quoting bytes). Fix to cql_quote(val) is correct.

Issues Found

# File Line Severity Finding
1 cassandra/cluster.py 5704 Medium Pre-existing bug: query_id.encode("hex") is a Python 2 idiom that raises TypeError on Python 3 (bytes has no .encode() that accepts "hex"). Should be query_id.hex(). This was not introduced by this PR (only reformatted), but since this PR is specifically about Py3 cleanup, it should be fixed here.
2 cassandra/util.py 1624 Low Missed stale TODO: TODO: when python2 support is removed, use packaging.version. — Python 2 support is being removed by this very PR. This TODO should either be acted on or updated. The "Update stale Python 2 comments" commit did not catch it.
3 cassandra/io/asyncioreactor.py 95 Low Missed cleanup: if sys.version_info[0] == 3 and sys.version_info[1] < 10: — the version_info[0] == 3 check is now redundant since minimum Python is 3.9+. Should be simplified to if sys.version_info < (3, 10):. This file was not modified by the PR.

Finding #1 is the most important — it's a latent runtime error that would crash if an unknown prepared statement is executed (error logging path).

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 35 out of 38 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cassandra/encoder.py
Comment thread setup.py
@mykaul mykaul force-pushed the python_2_no_more branch from 1a39140 to 4778136 Compare April 3, 2026 16:53
mykaul added 11 commits April 10, 2026 17:55
absolute_import is the default behavior in Python 3 and has been since
Python 3.0.  The import was only needed for Python 2 compatibility.
UnicodeMixin was a Python 2 compatibility shim that made __str__
delegate to __unicode__.  In Python 3, __str__ is the native method
and UnicodeMixin serves no purpose.

Affected classes: QueryValue, BaseQueryOperator, AbstractQueryableColumn,
ValueQuoter, BaseClause, BaseCQLStatement.
In Python 3, __str__ is the native string method.  The __unicode__
methods were a Python 2 convention used together with UnicodeMixin.

Affected files: statements.py, operators.py, named.py, models.py,
query.py, functions.py.
__nonzero__ was the Python 2 name for the boolean conversion method.
Python 3 uses __bool__ directly.
weakref.WeakSet has been available since Python 2.7 and is always
present in Python 3.  Remove the try/except fallback imports in
cluster.py, pool.py, and asyncorereactor.py, and delete the custom
_IterationGuard and WeakSet classes from util.py (~210 lines).
… 3.9+

- cluster.py: remove 'if sys.version_info >= 3.7' guard around eventlet
  check (always true since we require Python 3.9+).  Update error message.
- test_insights.py: remove 'if sys.version_info > (3,)' guard around
  namespace suffix (always true).
- test_row_factories.py: remove NAMEDTUPLE_CREATION_BUG flag and
  Python 3.0-3.6 warning path (bug was fixed in Python 3.7).
subprocess has been part of the standard library since Python 2.4.
Also fixes:
- Use sys.executable instead of hardcoded 'python' for subprocess calls
- Use 'python -m sphinx' instead of 'sphinx-build' for docs
- Fix get_subdriname() bug: was iterating over characters of a string
  path instead of calling os.listdir() on the path
- Remove duplicate is_macos definition
In Python 3, the u'' prefix on string literals is a no-op (str is
always unicode).  Remove 150 occurrences across 15 files.

Also update cql_encode_unicode() in encoder.py to pass the string
directly to cql_quote() instead of encoding to UTF-8 bytes first,
since Python 3 strings are always unicode.
- test_validation.py: remove 'if sys.version_info < (3, 1)' blocks that
  tested unichr() (only available in Python 2).  Also fix 'class
  DataType():' to 'class DataType:'.
- test_metadata.py: delete test_export_keyspace_schema_udts which was
  skipped on all Python versions except 2.7.
The workaround pre-loaded the UTF-8 encoding module to avoid a deadlock
when importing from multiple threads.  This bug was fixed in Python 3.3.
@mykaul mykaul force-pushed the python_2_no_more branch from 4778136 to 42e3d08 Compare April 10, 2026 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants