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

Can't use registered strategy for typed dict #2872

Closed
Melevir opened this issue Feb 23, 2021 · 1 comment
Closed

Can't use registered strategy for typed dict #2872

Melevir opened this issue Feb 23, 2021 · 1 comment
Labels
bug something is clearly wrong here

Comments

@Melevir
Copy link
Contributor

Melevir commented Feb 23, 2021

Here is a reproducible example of the problem:

# test.py
from typing import TypedDict, Callable, NamedTuple

from hypothesis import given
from hypothesis.strategies import composite, register_type_strategy, builds, from_type

class SomeData(TypedDict):
    a: str
    b: int

@composite
def some_data_composite(draw: Callable):
    assert 0  # to be sure when it works

register_type_strategy(SomeData, some_data_composite())

@given(from_type(SomeData))
def test_some_data(item):
    print(item)

I'm expecting pytest test.py to fail since there is assert 0 in some_data_composite strategy.

The test passes (hypothesis==6.3.0), but if I will replace class SomeData(TypedDict) with class SomeData(NamedTuple) or class SomeData, it will fail.

This happens because the TypedDict case is the first thing that is handled in hypothesis.strategies._internal.core._from_type, much earlier, than _global_type_lookup check.

This seems a problem for me since I'm using Hypothesis with Deal and I want to set up a default strategy for my typed dictionaries.

If this bug is confirmed, I'll try to fix it.

@Zac-HD Zac-HD added the bug something is clearly wrong here label Feb 23, 2021
@Zac-HD
Copy link
Member

Zac-HD commented Feb 23, 2021

Yep, that's a bug! We'll be happy to accept a PR to make this less hard-coded 😊

  • we'll also want to handle inheritance for TypedDicts - if resolving class SomeMoreData(SomeData):, the fields from SomeData should use whatever was registered for that type. It's OK to leave this for a future PR, of course - we're usually happy to take incremental improvements!
  • tip: enabling syntax highlighting on GitHub

(I'm also happy to give hints or spoilers about how I'd fix it, if you want/get stuck. Just let me know)

Melevir added a commit to Melevir/hypothesis that referenced this issue Feb 24, 2021
Melevir added a commit to Melevir/hypothesis that referenced this issue Feb 25, 2021
@Zac-HD Zac-HD closed this as completed in c7934f3 Feb 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something is clearly wrong here
Projects
None yet
Development

No branches or pull requests

2 participants