Skip to content

UnboundLocalError for new_scope #625

Closed

Description

@treykeown

Description

The issue here https://github.com/jcrist/msgspec/blob/4d4a02e62ee7b9ccc5dadaa2b0d1336279561bb4/msgspec/_utils.py#L88
If the else on line 75 is taken, but neither of the two inner if statements are true, new_scope is unset.

Traceback from msgspec:

    return msgspec.msgpack.decode(encoded, type=cls._decode_type)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/.../.venv/lib/python3.12/site-packages/msgspec/_utils.py", line 113, in get_class_annotations
    mro, typevar_mappings = _get_class_mro_and_typevar_mappings(obj)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/.../.venv/lib/python3.12/site-packages/msgspec/_utils.py", line 90, in _get_class_mro_and_typevar_mappings
    inner(obj, {})
  File "/Users/.../.venv/lib/python3.12/site-packages/msgspec/_utils.py", line 88, in inner
    inner(b, ...)
  File "/Users/.../.venv/lib/python3.12/site-packages/msgspec/_utils.py", line 88, in inner
    inner(b, new_scope)
  File "/Users/.../.venv/lib/python3.12/site-packages/msgspec/_utils.py", line 88, in inner
    inner(b, new_scope)
             ^^^^^^^^^
UnboundLocalError: cannot access local variable 'new_scope' where it is not associated with a value

I'll work on creating a minimal example.

Activity

  1. treykeown commented on Jan 4, 2024

    @treykeown
    Author
    import msgspec
    from typing import Generic, TypeVar
    
    T = TypeVar('T')
    T_int = TypeVar('T_int', bound=int)
    
    class Base(msgspec.Struct, Generic[T], tag=True):
        """..."""
    
    
    class Subclass(Base[int]):
        """..."""
    
    
    class Bug(Subclass, Base[T_int], Generic[T_int]):
        """..."""
    
    
    if __name__ == "__main__":
        enc = msgspec.msgpack.encode(Bug())
        dec = msgspec.msgpack.decode(enc, type=list[Bug])
        print(dec)
    Traceback (most recent call last):
      File "/Users/.../scratch.py", line 21, in <module>
        dec = msgspec.msgpack.decode(enc, type=list[Bug])
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/Users/.../.venv/lib/python3.12/site-packages/msgspec/_utils.py", line 113, in get_class_annotations
        mro, typevar_mappings = _get_class_mro_and_typevar_mappings(obj)
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/Users/.../.venv/lib/python3.12/site-packages/msgspec/_utils.py", line 90, in _get_class_mro_and_typevar_mappings
        inner(obj, {})
      File "/Users/.../.venv/lib/python3.12/site-packages/msgspec/_utils.py", line 88, in inner
        inner(b, new_scope)
      File "/Users/.../.venv/lib/python3.12/site-packages/msgspec/_utils.py", line 88, in inner
        inner(b, new_scope)
                 ^^^^^^^^^
    UnboundLocalError: cannot access local variable 'new_scope' where it is not associated with a value
    
  2. treykeown commented on Jan 4, 2024

    @treykeown
    Author

    I've since fixed how my classes are structured, but this is still an issue any time a generic class appears both in a parent class and in a child class.

  3. jcrist commented on Jan 5, 2024

    @jcrist
    Member

    Thanks for the excellent reproducible issue, this should be fixed by #626.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions