Search code examples
pythonunit-testingpytestpython-typing

How to tell python that the unexpected variable type passing to a function is correct in the context


I'm new to Python 3 and testing with pytest. Sometimes, I want to test that my function raises TypeError or AssertionError when I provide it with type that it is not supposed to process. Consider this simple example:

import pytest


def concatenate_string(first_string: str, second_string: str) -> str:
    return first_string + second_string


def test_concatenate_string_raises_type_error():
    with pytest.raises(TypeError) as pytest_wrapped_e:
        concatenate_string('abc', 1)
    assert e.type == TypeError

My problem here is, that when I provide the integer 1 to the function concatenate_string, PyCharm warns me about unexpected type assigned. I know this is a correct behaviour, but is there a way to tell the program (e.g. by some other annotation): "I know what I'm doing, I really want to have this type here, don't warn be about it."?

I know I can just tell the editor to ignore it, but it feels somehow wrong to me.


Solution

  • From here:

    concatenate_string('abc', 1)  # type: ignore
    

    this should suppress the type checking for that line