agronholm / typeguard

Run-time type checker for Python
Other
1.52k stars 114 forks source link

Typecheck apparently only checks the first element in a list #430

Closed malterippa1 closed 9 months ago

malterippa1 commented 9 months ago

Things to check first

Typeguard version

4.1.5

Python version

3.11.5

What happened?

When using the @typechecked decorator to assure that a function accepts a list of strings as input (e.g. input_a : list[str]) a typeguard.TypeCheckError is raised only when the first element of the list does not match the requested type.

Example: from typeguard import typechecked

@typechecked def run(input_sequence_list: list[str]) -> None: print(input_sequence_list)

run(input_sequence_list=[1, 2, 3, 22])

Output: typeguard.TypeCheckError: item 0 of argument "input_sequence_list" (list) is not an instance of str

The following example does not raise an error:

from typeguard import typechecked

@typechecked def run(input_sequence_list: list[str]) -> None: print(input_sequence_list)

run(input_sequence_list=["1", 2, 3, 22])

Output: ['1', 2, 3, 22]

Is this intended behaviour?

How can we reproduce the bug?

Install python 3.11.5 pip install typeguard==4.1.5

run the code below.

from typeguard import typechecked @typechecked def run(input_sequence_list: list[str]) -> None: print(input_sequence_list)

run(input_sequence_list=["1", 2, 3, 22])