Skip to content

Fix Python 3.12 compatibility issues: escape sequences, builtin signatures, and error messages#60

Merged
thorwhalen merged 3 commits intomasterfrom
copilot/fix-dol-for-python-3-12
Oct 15, 2025
Merged

Fix Python 3.12 compatibility issues: escape sequences, builtin signatures, and error messages#60
thorwhalen merged 3 commits intomasterfrom
copilot/fix-dol-for-python-3-12

Conversation

Copy link
Contributor

Copilot AI commented Oct 15, 2025

Overview

This PR fixes all Python 3.12 compatibility issues identified in #XX (link to issue). All 366 tests now pass successfully with no syntax warnings.

Issues Fixed

1. Invalid Escape Sequence Warnings

Python 3.12 enforces stricter escape sequence handling in string literals. Docstrings containing regex patterns like \d and \w were causing SyntaxWarning errors.

Fixed by:

  • Converting docstrings to raw strings (r"""...""") in dol/paths.py, dol/trans.py
  • Using raw strings in doctest examples throughout dol/paths.py and dol/tests/test_paths.py

2. Doctest Collection Error with Decorated Classes

The CachedSequenceKvReader class was causing an AttributeError during doctest collection:

AttributeError: type object 'CachedSequenceKvReader' has no attribute '__code__'

Root cause: In dol/caching.py, the @wraps(store) decorator on __init__ was incorrectly wrapping with the class instead of the class's __init__ method, causing inspect.unwrap() to fail.

Fixed by: Changed @wraps(store) to @wraps(store.__init__)

3. Builtin Signature Changes

Python 3.12 now provides signatures for many builtins that previously didn't have them (like print), breaking doctests that expected has_signature(print) to return False.

Fixed by:

  • Updated doctests in dol/signatures.py to use map instead of print (map still has no signature)
  • Updated expected count in has_signature test from 2 to 3

4. Error Message Changes

Python 3.12 changed TypeError messages for keyword-only arguments from:

TypeError: missing a required argument: 'w'

to:

TypeError: missing a required keyword-only argument: 'w'

Fixed by: Updated expected error messages in multiple signature doctests

5. OrderedDict Repr Changes

Python 3.12 changed OrderedDict repr format from OrderedDict([('key', 42)]) to OrderedDict({'key': 42}).

Fixed by: Updated doctest to use # doctest: +ELLIPSIS with flexible pattern matching

6. UserDict Behavior Clarification

Updated CascadedStores doctest to reflect actual behavior where dict.get() doesn't trigger __getitem__ when key is missing.

Test Results

  • ✅ 366 tests passed
  • ⏭️ 2 tests skipped (pre-existing, unrelated)
  • ⚠️ 1 warning (pre-existing test collection warning, unrelated)
  • ❌ 0 failures
  • 🎯 0 syntax warnings

Files Changed

  • dol/caching.py - Fixed @wraps decorator usage
  • dol/paths.py - Raw string docstrings and doctest updates
  • dol/signatures.py - Updated for builtin signature changes and error messages
  • dol/sources.py - Fixed doctest expectation
  • dol/tests/test_paths.py - Raw string literals in tests
  • dol/trans.py - Raw string docstring

All changes are minimal and surgical, focused solely on Python 3.12 compatibility without altering functionality.

Original prompt

This section details on the original issue you should resolve

<issue_title>Make dol work in 3.12</issue_title>
<issue_description>I get a bunch of "escape characters" when I test dol in python 3.12
See the CI run here:
https://github.com/i2mint/dol/actions/runs/18514726651/job/52762756139

Run the tests in python 3.12 (including doctests) and make them pass.

Here's some things you might try (but take with a grain of salt):

Here are focused solutions for the failing tests in your job:


1. dol/signatures.py::dol.signatures.Sig.sig_or_none

Failure: This test failed.
Solution:

  • Review the implementation of sig_or_none. Ensure it correctly handles cases where a signature may not exist (e.g., for built-in functions or objects lacking a __signature__).
  • Example fix:
    import inspect
    
    def sig_or_none(obj):
        try:
            return inspect.signature(obj)
        except (ValueError, TypeError):
            return None
  • Check the test for cases it expects to return None and confirm coverage.

2. dol/signatures.py::dol.signatures.has_signature

Failure: This test failed.
Solution:

  • Ensure has_signature returns True only if inspect.signature(obj) does not raise an exception.
  • Example fix:
    import inspect
    
    def has_signature(obj):
        try:
            inspect.signature(obj)
            return True
        except (ValueError, TypeError):
            return False

3. dol/paths.py::dol.paths.path_set

Failure: This test failed.
Solution:

  • Check the implementation of path_set. Confirm it can set a value at a path correctly (e.g., in files or dictionaries).
  • If the test expects the function to create directories or handle missing paths, ensure those cases are handled.
  • Example fix:
    import os
    
    def path_set(path, value):
        os.makedirs(os.path.dirname(path), exist_ok=True)
        with open(path, 'w') as f:
            f.write(value)

4. dol/sources.py::dol.sources.CascadedStores

Failure: This test failed.
Solution:

  • Inspect the logic for cascading between stores (e.g., reading from one, falling back to another).
  • Ensure correct method resolution order and error handling if a key is missing.
  • Example fix:
    class CascadedStores:
        def __init__(self, *stores):
            self.stores = stores
    
        def __getitem__(self, key):
            for store in self.stores:
                try:
                    return store[key]
                except KeyError:
                    continue
            raise KeyError(key)

General Recommendations:

  • Fix the old pip invocation warning: Replace direct pip calls with python -m pip in your scripts.
  • Address the invalid escape sequence '\\d' warning by using raw strings in regex patterns:
    pattern = r"\d+"

</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #59


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits October 15, 2025 01:36
Co-authored-by: thorwhalen <1906276+thorwhalen@users.noreply.github.com>
Co-authored-by: thorwhalen <1906276+thorwhalen@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix dol to work with Python 3.12 Fix Python 3.12 compatibility issues: escape sequences, builtin signatures, and error messages Oct 15, 2025
Copilot AI requested a review from thorwhalen October 15, 2025 01:44
@thorwhalen thorwhalen marked this pull request as ready for review October 15, 2025 02:24
@thorwhalen thorwhalen merged commit cb78bea into master Oct 15, 2025
9 checks passed
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.

Make dol work in 3.12

2 participants