Skip to content

Can't wrap function with StrEnum as argument with default #98

Closed

Description

@AlexeyDmitriev

This issue is similar to #63

from enum import StrEnum
from makefun import wraps

class A(StrEnum):
    a = 'a'

def foo(a=A.a):
    pass

@wraps(foo)
def test(*args, **kwargs):
    return foo(*args, **kwargs)

When this runs it throws:

Error in generated code:
def foo(a=<A.a: 'a'>):
    return _func_impl_(a=a)

Traceback (most recent call last):
  File "/home/alexeyd/a.py", line 10, in <module>
    @wraps(foo)
     ^^^^^^^^^^
  File "/usr/python3.11/lib/python3.11/site-packages/makefun/main.py", line 1062, in replace_f
    return create_function(func_signature=func_signature,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/python3.11/lib/python3.11/site-packages/makefun/main.py", line 323, in create_function
    f = _make(co_name, params_names, body, evaldict)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/python3.11/lib/python3.11/site-packages/makefun/main.py", line 714, in _make
    code = compile(body, filename, 'single')
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<makefun-gen-0>", line 1
    def foo(a=<A.a: 'a'>):
              ^
SyntaxError: invalid syntax

The problem is StrEnum is also an str (that's a point of the class), so here it is assumed to have safe repr.

I propose to change the check from checking isinstance to checking type(symbol) in TYPES_WITH_SAFE_REPR

Activity

  1. changed the title [-]Can't wrap function with StrEnum as argument[/-] [+]Can't wrap function with StrEnum as argument with default[/+] on Jan 23, 2024
  2. added a commit that references this issue on Jan 25, 2024
    bd5323e
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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions