Skip to content

Remove Python 2 / pre-3.7 era idioms - #1047

Merged
xpconanfan merged 2 commits into
masterfrom
modernize-py2-idioms
Oct 7, 2026
Merged

xpconanfan merged 2 commits into
masterfrom
modernize-py2-idioms

Conversation

@xpconanfan

@xpconanfan xpconanfan commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator

Mobly requires Python >= 3.11; this drops leftover Python 2 / pre-3.7 compatibility idioms. No behavior change.

  • io.open -> open; drop from builtins import str.
  • str(b, 'utf-8') -> b.decode('utf-8').
  • collections.OrderedDict() -> {} (dicts are ordered since 3.7); extra_errors is now a plain dict.
  • create_dir: os.makedirs(..., exist_ok=True) instead of the errno/EEXIST dance.
  • Drop the Python 2 text/bytes branch in ExceptionRecord.
  • nonlocal instead of the one-element-list closure hack.
  • python3.4 shebang -> python3; drop u'' literal and coding cookie.

@xpconanfan xpconanfan self-assigned this Oct 6, 2026
@xpconanfan xpconanfan added this to the Mobly Release 1.14 milestone Oct 6, 2026
Mobly requires Python >= 3.11, so a number of compatibility idioms from the
Python 2 and early Python 3 days are now pure noise. This change removes
them without altering behavior:

* `io.open(...)` -> `open(...)` (identical since Python 3.0), and drops the
  now-unused `import io` (config_parser, records, utils, iperf_server).
* `from builtins import str` (the `future` package shim) in config_parser.
* `str(bytes, 'utf-8')` -> `bytes.decode('utf-8')` (android_device, adb,
  snippet_client_v2).
* `collections.OrderedDict()` -> `{}` where only insertion order is relied on
  (guaranteed since 3.7): base_test, suite_runner, controller_manager,
  service_manager, records. `TestResultRecord.extra_errors` is now a plain
  dict; its `popitem(last=False)` use is replaced with `next(iter(...))`.
  Also removes the "OrderedDict#items does not return a sequence in
  Python 3.4" comments and the `list(...)` copies they justified.
* `utils.create_dir`: replace the EEXIST/errno dance with
  `os.makedirs(..., exist_ok=True)`; drop the "does not support Python 2"
  docstring note.
* `base_instrumentation_test`: replace the one-element-list closure hack
  with `nonlocal`.
* `records._set_details`: drop the Python 2 text-vs-bytes special casing;
  `str(content)` is correct for all inputs on Python 3.
* `#!/usr/bin/env python3.4` -> `python3`, drop `u''` literal
  (tools/snippet_shell.py); drop the `coding: utf-8` cookie (docs/conf.py).

No functional change; the full unit test suite passes unchanged.
@xpconanfan
xpconanfan force-pushed the modernize-py2-idioms branch from 1c32821 to fe7fb79 Compare October 7, 2026 04:48
@xpconanfan
xpconanfan merged commit 59333a2 into master Oct 7, 2026
35 checks passed
@xpconanfan
xpconanfan deleted the modernize-py2-idioms branch October 7, 2026 04:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants