[Python-ideas] Check type hints in stack trace printing

Daniel Sánchez Fábregas daniel.sanchez.fabregas at xunta.gal
Wed Jun 20 05:43:08 EDT 2018


El 14/06/18 a las 14:37, Steven D'Aprano escribió:
> On Thu, Jun 14, 2018 at 01:03:37PM +0200, Daniel Sánchez Fábregas wrote:
>> My idea consist in:
>> Adding a method to perform type checking in traceback objects
>> When printing stack traces search for mistyped arguments and warn about
>> them to the user.
> Can you give a concrete example of how this would work?
>
Example on how this should work from the user point of view:

~~~ python

def one(arg: str) -> str:
    return two(arg) + "1"
def two(arg: str) -> str:
    return three(arg) * 2
def three(arg: str) -> str:
    return "{}({}) ".format(arg, len(arg))

print(one("test"))
print(one(0))

~~~

Intended output:

~~~

test(4) test(4) 1
Traceback (most recent call last):
  File "test.py", line 9, in <module>
    print(one(0))
  Warning: TypeMistmatch argument 'arg' of type 'int' is declared as 'str'
  File "test.py", line 2, in one
    return two(arg) + "1"
  Warning: TypeMistmatch argument 'arg' of type 'int' is declared as 'str'
  File "test.py", line 4, in two
    return three(arg) * 2
  Warning: TypeMistmatch argument 'arg' of type 'int' is declared as 'str'
  File "test.py", line 6, in three
    return "{}({}) ".format(arg, len(arg))
TypeError: object of type 'int' has no len()

~~~

How could it be achieved? I don't know enough python to answer this. I
suppose that it could be done.



More information about the Python-ideas mailing list