Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for interleaved errors #23

Open
ikonst opened this issue Jan 1, 2019 · 1 comment
Open

Support for interleaved errors #23

ikonst opened this issue Jan 1, 2019 · 1 comment

Comments

@ikonst
Copy link

ikonst commented Jan 1, 2019

Here's what I came up with while trying to replicate mypy's own testcases, which interleave the expected errors in the source code like so:
https://github.com/ikonst/pynamodb-mypy/blob/9453404f576c4ead04749d61eb041e10bec13229/tests/test_plugin.py

The implementation is here:
https://github.com/ikonst/pynamodb-mypy/blob/9453404f576c4ead04749d61eb041e10bec13229/tests/mypy_helpers.py

In a failure case, the failed assertion output will look like:

>       assert actual == expected
E       assert 'from pynamod...ibute "lower"' == 'from pynamodb..._attr.lower()'
E           from pynamodb.attributes import UnicodeAttribute
E           from pynamodb.models import Model
E
E           class MyModel(Model):
E               my_attr = UnicodeAttribute(null=True)
E
E           reveal_type(MyModel.my_attr)  # E: Revealed type is 'pynamodb.attributes._NullableAttribute[pynamodb.attributes.UnicodeAttribute, builtins.str]'
E           reveal_type(MyModel().my_attr)  # E: Revealed type is 'Union[builtins.str*, None]'
E         - MyModel().my_attr.lower()  # E: Item "None" of "Optional[str]" has no attribute "lower"
E         + MyModel().my_attr.lower()

Of course, this requires assertion rewriting, which would've been done in the pytest plugin normally:
https://github.com/ikonst/pynamodb-mypy/blob/9453404f576c4ead04749d61eb041e10bec13229/tests/__init__.py#L3

There are two advantages of interleaving the errors in the test code:

  1. changing the test code does not necessitate updating of line numbers
  2. updating the test code is easier (by diffing the program against the "expected program", i.e. data-driven testing)

Have you considered allowing a similar approach?

@dmtucker
Copy link
Collaborator

dmtucker commented Aug 1, 2019

I had not, but it's a neat idea!
I started playing with it locally a little... Seems like it could be an interesting/useful option 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants