timer icon indicating copy to clipboard operation
timer copied to clipboard

Timer object doesn't play nice with Fire

Open christophevg opened this issue 3 years ago • 0 comments

When using timer in combination with the Fire the Timer object misses a name when using the @timer annotation.

Example:

import logging
logging.basicConfig(level=logging.DEBUG)

import time

from timer import timer

import fire

class Test():
  
  @timer
  def fail_missing_name(self):
    time.sleep(2)
    print("ok")

  @timer()
  def doesnt_fail_missing_name(self):
    time.sleep(2)
    print("ok")

if __name__ == "__main__":
  fire.Fire(Test)
  % pip install fire

  % python fire_bug.py fail_missing_name 
  Traceback (most recent call last):
    File "fire_bug.py", line 23, in <module>
      fire.Fire(Test)
    File "/Users/xtof/.pyenv/versions/timer/lib/python3.8/site-packages/fire/core.py", line 141, in Fire
      component_trace = _Fire(component, args, parsed_flag_args, context, name)
    File "/Users/xtof/.pyenv/versions/timer/lib/python3.8/site-packages/fire/core.py", line 471, in _Fire
      target=component.__name__)
  AttributeError: 'Timer' object has no attribute '__name__'
  
  % python fire_bug.py doesnt_fail_missing_name
  DEBUG:timer.doesnt_fail_missing_name:start
  ok
  DEBUG:timer.doesnt_fail_missing_name:cost 2.004 s

(I have forked the repo and will submit a PR fixing this issue.)

christophevg avatar Jun 19 '22 09:06 christophevg