Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Enforce throws TypeError when calling instance method with forgotten self #49

Open
justuswilhelm opened this issue Aug 16, 2017 · 1 comment

Comments

@justuswilhelm
Copy link

I have had this obscure problem, which I would call a bug since one would expect to get a proper error message like Python 3 normally does.

I use

  • Python 3.6.1 and
  • enforce==0.3.4
class A:
    def hello():
        print("Hello, World!")

a = A()
# Traceback (most recent call last):
#   File "example.py", line 13, in <module>
#     a.hello()
#   File "example.py", line 4, in hello
#     print("Hello, " + b.world())
# TypeError: world() takes 0 positional arguments but 1 was given
try:
    a.hello()
except TypeError:
    print("Error caught correctly")


# But now

import enforce

@enforce.runtime_validation
class BadA:
    def hello():
        print("Hello, ")

a = BadA()
a.hello()

will output

Error caught correctly   
Traceback (most recent call last):                
  File "example.py", line 30, in <module>         
    a.hello()            
  File "/Users/XXX/enforce-test/env/lib/python3.6/site-packages/enforce/decorators.py", line 104, in universal
    _args, _kwargs, _ = enforcer.validate_inputs(parameters)                                         
  File "/Users/XXX/enforce-test/env/lib/python3.6/site-packages/enforce/enforcers.py", line 71, in validate_inputs
    binded_arguments = self.signature.bind(*args, **kwargs)                                          
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/inspect.py", line 2933, in bind
    return args[0]._bind(args[1:], kwargs)        
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/inspect.py", line 2854, in _bind
    raise TypeError('too many positional arguments') from None                                       
TypeError: too many positional arguments    
@RussBaz
Copy link
Owner

RussBaz commented Aug 17, 2017

As a rule of thumb, one should not expect to get the same exception message from the decorated function as if it was not.

It still gives the correct exception type, however, but totally unintuitive stacktrace.

I agree this seems like a very likely place to run into exceptions. I will have a look if anything can be done about the clarity of such exceptions at this moment.

Thanks.

RussBaz added a commit that referenced this issue Nov 18, 2017
…rty to an Enforcer class. This property holds the name of the object to which the Enforcer was applied.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants