cyberdelia / metrology

A library to easily measure what's going on in your python.
https://metrology.readthedocs.org
MIT License
309 stars 25 forks source link

Decorators don't allow for return values #34

Closed steven-lai closed 7 years ago

steven-lai commented 7 years ago
@Metrology.timer('test')
def test():
  return 'test'

print(test())

Expected output is test but instead None is printed.

I believe the relevant code in metrology.instruments.meter.Meter:

    def __call__(self, *args, **kwargs):
        if args and hasattr(args[0], '__call__'):
            _orig_func = args[0]
            def _decorator(*args, **kwargs):
                with self:
                    _orig_func(*args, **kwargs)
            return _decorator

It should say return _orig_func(*args, **kwargs) instead, maybe?

Using metrology 1.2.2 with Python 2.7.13

steven-lai commented 7 years ago

My current workaround is:

def test():
  with Metrology.timer('test'):
    return 'test'
cyberdelia commented 7 years ago

@steven-lai Looks like so, would you mind making a PR for both meter and timer?

steven-lai commented 7 years ago

@cyberdelia PR submitted - https://github.com/cyberdelia/metrology/pull/35

steven-lai commented 7 years ago

@cyberdelia I saw you released 1.2.3 and I assume this change is in there, please correct me if I'm wrong.

Upon updating to 1.2.3, I'm still seeing the same issue:

cyberdelia commented 7 years ago

@steven-lai Try with 1.2.4?

steven-lai commented 7 years ago

Downloaded the wheel, unpacked, and verified the code has the changes. Thanks!