typeddjango / pytest-mypy-plugins

pytest plugin for testing mypy types, stubs, and plugins
https://pypi.org/project/pytest-mypy-plugins/
MIT License
102 stars 24 forks source link

Incorrect output formatting for Expected #64

Closed zero323 closed 3 years ago

zero323 commented 3 years ago

Expected block is not correctly formatted with all lines printed as <45 (diff). For example if I try to test:

- case: failing_case_1
  main: |
    reveal_type(42)  # N: Revealed type is "builtins.str"
    reveal_type("foo")  # N: Revealed type is "builtins.int"

I get

/path/to/test-file.yaml:3:
E   pytest_mypy_plugins.utils.TypecheckAssertionError: Invalid output: 
E   Expected:
E     <45 (diff)
E     <45 (diff)
E   Actual:
E     main:1: note: Revealed type is "Literal[42]?" (diff)
E     main:2: note: Revealed type is "Literal['foo']?" (diff)
E   
E   Alignment of first line difference:
E     E: ...te: Revealed type is "builtins.str"
E     A: ...te: Revealed type is "Literal[42]?"
E                                 ^

while I expect

/path/to/test-file.yaml:3:
E   pytest_mypy_plugins.utils.TypecheckAssertionError: Output is not expected: 
E   Actual:
E     main:1: note: Revealed type is "Literal[42]?" (diff)
E     main:2: note: Revealed type is "Literal['foo']?" (diff)
E   Expected:
E     main:1: note: Revealed type is "builtins.str" (diff)
E     main:2: note: Revealed type is "builtins.int" (diff)
E   Alignment of first line difference:
E     E: ...te: Revealed type is "builtins.str"
E     A: ...te: Revealed type is "Literal[42]?"

This happens because OutputMatcher is fed directly into str.format, instead of converting it first to str

https://github.com/typeddjango/pytest-mypy-plugins/blob/f7b249d0e40c25e2979adfbdb804a2e489072837/pytest_mypy_plugins/utils.py#L254

See for example:

>>> from pytest_mypy_plugins.utils import OutputMatcher
>>>
>>> expected = OutputMatcher("foo", 1, "E", "bar", False)
>>> "  {:<45} (diff)".format(expected)
## '  <45 (diff)'
>>> "  {:<45} (diff)".format(str(expected)) 
'  foo:1: E: bar                                 (diff)'

Environment:

sobolevn commented 3 years ago

@zero323 PR is very welcome! 👍