Python type input error on wrong line

I'm not sure if the problem I am facing is a lack of experience with Python or an error in the interpreter, but I think that I am receiving a TypeError message on the wrong line.

Please explain to me why this happens if it is not a mistake. My code is as follows:

 #!/usr/bin/env python3 from awacs.aws import Policy, Principal, Statement from troposphere import Template from troposphere.iam import Role t = Template() t.add_resource(Role( "SESLambdaRole", AssumeRolePolicyDocument = Policy( Version = "2012-10-17", Statement = [ Statement( Effect = "Allow", Resource = "arn:aws:logs:*:*:*", Principal = Principal( "Service", ["lambda.amazonaws.com"] ), ) ] ) )) print(t.to_json()) 

This is my conclusion.

 ubuntu@ip-111-11-11-111 :~$ ./ses-lambda-forwarder-resources.py Traceback (most recent call last): File "./ses-lambda-forwarder-resources.py", line 19, in <module> ["lambda.amazonaws.com"] File "/home/ubuntu/.local/lib/python3.6/site-packages/awacs/__init__.py", line 113, in __init__ sup.__init__(None, props=self.props, **kwargs) File "/home/ubuntu/.local/lib/python3.6/site-packages/awacs/__init__.py", line 40, in __init__ self.__setattr__(k, v) File "/home/ubuntu/.local/lib/python3.6/site-packages/awacs/__init__.py", line 81, in __setattr__ self._raise_type(name, value, expected_type) File "/home/ubuntu/.local/lib/python3.6/site-packages/awacs/__init__.py", line 90, in _raise_type (name, type(value), expected_type)) TypeError: Resource is <class 'str'>, expected <class 'list'> ubuntu@ip-111-11-11-111 :~$ python3 --version Python 3.6.3 

If I change the following line

 Resource = "arn:aws:logs:*:*:*", 

to

 Resource = [ "arn:aws:logs:*:*:*" ], 

It works. Why is Python complaining about line 3 of the line below?

+5
source share
1 answer

Python does not know which actual argument is wrong, it was just passed from an expression that extends over several lines. He makes an approximate assumption of an error (which, as Ryan points out in the comments , is the last line without punctuation in the expression) and reports the error as coming from there, but it could have come from any part of the expression that was an error. For a simpler example, consider:

 >>> i = int( ... '#', ... base=2 ... ) ... --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-6-1e4a194d0c31> in <module>() 1 i = int( 2 '#', ----> 3 base=2 4 ) 5 ValueError: invalid literal for int() with base 2: '#' 

The error comes from the int constructor (the call starts at line 1), from the argument '#' (line 2), and the expression ends at line 4, but the arrow means line 3. All Python really knows is that the error came from an int(...) expression int(...) , and this causes the problem, which is the final semantic line of the expression, but does not really know which of the arguments was to blame (exception collector, t thin enough to programmatically determine Python, which argument is the problem in a way that can help), and heuristic is not working melts.

The same thing happens with your code; Python knows that the Statement constructor raised an exception and points to the last non-trivial line of this expression when an error occurs, but in fact it does not know which line contains the problematic argument, and the heuristic gives you misleading Information. Fortunately, the exception message tells you what is wrong (the Resource argument must be list ), so you can use this context to scan nearby lines and notice the list Resource argument.

+1
source

Source: https://habr.com/ru/post/1275249/


All Articles