simonpercivall / astunparse

An AST unparser for Python
Other
223 stars 53 forks source link

internal tests failing under Python 3.11 #67

Open tijuca opened 1 year ago

tijuca commented 1 year ago

While Python 3.11 is now the default Python version within the upcoming Debian release bookworm it turns out that the package astunparse can't run the internal tests since the switch to Python 3.11. This has become visible within a complete rebuild of all package within the archive.

There is an issue raised due the package rebuild problem against the package in astunparse in the Debian BTS within #1026643

The relevant part about the issue should be

> I: pybuild base:240: cd /<<PKGBUILDDIR>>/.pybuild/cpython3_3.11_astunparse/build; python3.11 -m pytest tests
> ============================= test session starts ==============================
> platform linux -- Python 3.11.1, pytest-7.2.0, pluggy-1.0.0+repack
> rootdir: /<<PKGBUILDDIR>>
> collected 94 items
> 
> tests/test_dump.py ........................s.s.s......s...........       [ 50%]
> tests/test_unparse.py .................F......s.s.s......s...........    [100%]
> 
> =================================== FAILURES ===================================
> __________________________ UnparseTestCase.test_files __________________________
> 
> self = <tests.test_unparse.UnparseTestCase testMethod=test_files>
> 
>     def test_files(self):
>         names = []
>         for test_dir in self.test_directories:
>             for n in os.listdir(test_dir):
>                 if n.endswith('.py') and not n.startswith('bad'):
>                     names.append(os.path.join(test_dir, n))
>     
>         for filename in names:
>             print('Testing %s' % filename)
>             source = read_pyfile(filename)
> >           self.check_roundtrip(source)
> 
> tests/common.py:192: 
> _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
> tests/test_unparse.py:18: in check_roundtrip
>     code2 = astunparse.unparse(ast1)
> astunparse/__init__.py:13: in unparse
>     Unparser(tree, file=v)
> astunparse/unparser.py:38: in __init__
>     self.dispatch(tree)
> astunparse/unparser.py:66: in dispatch
>     meth(tree)
> astunparse/unparser.py:78: in _Module
>     self.dispatch(stmt)
> astunparse/unparser.py:66: in dispatch
>     meth(tree)
> astunparse/unparser.py:347: in _FunctionDef
>     self.__FunctionDef_helper(t, "def")
> astunparse/unparser.py:365: in __FunctionDef_helper
>     self.dispatch(t.body)
> astunparse/unparser.py:63: in dispatch
>     self.dispatch(t)
> _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
> 
> self = <astunparse.unparser.Unparser object at 0x7f5def4f5090>
> tree = <ast.Match object at 0x7f5def4cdb40>
> 
>     def dispatch(self, tree):
>         "Dispatcher function, dispatching tree type T to method _T."
>         if isinstance(tree, list):
>             for t in tree:
>                 self.dispatch(t)
>             return
> >       meth = getattr(self, "_"+tree.__class__.__name__)
> E       AttributeError: 'Unparser' object has no attribute '_Match'
> 
> astunparse/unparser.py:65: AttributeError
> Testing /usr/lib/python3.11/abc.py
> ... [snip]
> Testing /usr/lib/python3.11/traceback.py
> =============================== warnings summary ===============================
> .pybuild/cpython3_3.11_astunparse/build/tests/test_unparse.py::UnparseTestCase::test_files
>   internal:134: DeprecationWarning: invalid escape sequence '\Z'
> 
> .pybuild/cpython3_3.11_astunparse/build/tests/test_unparse.py::UnparseTestCase::test_files
>   internal:59: DeprecationWarning: invalid escape sequence '\P'
> 
> .pybuild/cpython3_3.11_astunparse/build/tests/test_unparse.py::UnparseTestCase::test_files
>   internal:166: DeprecationWarning: invalid escape sequence '\P'
> 
> .pybuild/cpython3_3.11_astunparse/build/tests/test_unparse.py::UnparseTestCase::test_files
>   internal:166: DeprecationWarning: invalid escape sequence '\s'
> 
> -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
> =========================== short test summary info ============================
> FAILED tests/test_unparse.py::UnparseTestCase::test_files - AttributeError: '...

Found out by some T&E it seems that the issue is triggered by usr/lib/python3.11/traceback.py and at least /usr/lib/python3.11/dataclasses.py too.

I hereby would like to get some help to fix the problem. Currently there a two patches applied to the source of version 1.6.3 before the package is build. https://sources.debian.org/src/astunparse/1.6.3-1/debian/patches/

Thanks!

stefan6419846 commented 1 year ago

In theory, no current Python package supporting Python since version 3.9 should be required to use this package, as the functionality is available in the stdlib now: https://docs.python.org/3/library/ast.html#ast.unparse https://github.com/python/cpython/commit/27fc3b6f3fc49a36d3f962caac5c5495696d12ed