Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

class callables can't be passed to lfc #28

Closed
adambudziak opened this issue Jan 23, 2025 · 0 comments · Fixed by #29
Closed

class callables can't be passed to lfc #28

adambudziak opened this issue Jan 23, 2025 · 0 comments · Fixed by #29

Comments

@adambudziak
Copy link

adambudziak commented Jan 23, 2025

Example

>>> from operator import attrgetter
>>> from pytest_lazy_fixtures import lfc
>>> lfc(attrgetter('id'))

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/XXX/.venv/lib/python3.13/site-packages/pytest_lazy_fixtures/lazy_fixture_callable.py", line 33, in lfc
    return LazyFixtureCallableWrapper(name, *args, **kwargs)
  File "/XXX/.venv/lib/python3.13/site-packages/pytest_lazy_fixtures/lazy_fixture_callable.py", line 16, in __init__
    self.name = func_or_name.__name__
                ^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'operator.attrgetter' object has no attribute '__name__'. Did you mean: '__ne__'?

It seems that attrgetter doesn't have __name__. I don't know what the name is used for, but one solution is to use __class__.__name__ instead.

attrgetter is not the only problematic callable, the same happens for itemgetter and partial, I seems it's the case for all class-based callables:

>>> class E:
...     def __call__(self, *args, **kwargs):
...         print('hello')
        
>>> e = E()
>>> e.__name__
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'E' object has no attribute '__name__'. Did you mean: '__ne__'?

I changed line 16 in lazy_fixture_callable.py to

try:
    self.name = func_or_name.__name__
except AttributeError:
    self.name = func_or_name.__class__.__name__

and my test using attrgetter passes, so maybe it's good enough

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant