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

Type-hinting of filter regression #4269

Open
dycw opened this issue Feb 17, 2025 · 3 comments
Open

Type-hinting of filter regression #4269

dycw opened this issue Feb 17, 2025 · 3 comments

Comments

@dycw
Copy link

dycw commented Feb 17, 2025

Hi all,

I have noticed a regression in the type-hinting of filter. More precisely, on 6.125.0:

from hypothesis.strategies import booleans

reveal_type(booleans().filter(lambda n: n))     ■ Undefined name `reveal_type`Type of "booleans().filter(lambda n: n)" is "SearchStrategy[bool]"

But on 6.125.1:

from hypothesis.strategies import booleans

reveal_type(booleans().filter(lambda n: n))     ■ Undefined name `reveal_type`     ■■■ Return type of lambda is unknown

I suppose it must be in hypothesis-python-6.125.0...hypothesis-python-6.125.1 ?

@tybug
Copy link
Member

tybug commented Feb 17, 2025

hmm, both pyright and mypy report this as SearchStrategy[bool] for me:

from hypothesis import strategies as st

reveal_type(st.booleans().filter(lambda n: n))
/Users/tybug/Desktop/sandbox.py:3: note: Revealed type is "hypothesis.strategies._internal.strategies.SearchStrategy[builtins.bool]" # mypy
/Users/tybug/Desktop/sandbox.py:3:13 - information: Type of "st.booleans().filter(lambda n: n)" is "SearchStrategy[bool]" # pyright

mypy 1.15.0 (compiled: yes), pyright pyright 1.1.393. Are you using another type checker?

@dycw
Copy link
Author

dycw commented Feb 17, 2025

Hi @tybug ; I'm using pyright and this is probably due to strictness.

setup

~/work/hypothesis via 🐍 v3.11.10 (python-3.11)which python
/Users/derekwan/work/hypothesis/.direnv/python-3.11/bin/python

~/work/hypothesis via 🐍 v3.11.10 (python-3.11)uv pip install hypothesis
Using Python 3.11.10 environment at: .direnv/python-3.11
Resolved 3 packages in 62ms
Installed 3 packages in 7ms
 + attrs==25.1.0
 + hypothesis==6.125.3
 + sortedcontainers==2.4.0

~/work/hypothesis via 🐍 v3.11.10 (python-3.11)which pyright
/opt/homebrew/bin/pyright

~/work/hypothesis via 🐍 v3.11.10 (python-3.11)pyright --version
pyright 1.1.393

~/work/hypothesis via 🐍 v3.11.10 (python-3.11)cat foo.py
───────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: foo.py
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ from hypothesis.strategies import booleans
   2   │
   3   │ reveal_type(booleans().filter(lambda n: n))
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

In normal mode:

~/work/hypothesis via 🐍 v3.11.10 (python-3.11)
❯ cat pyproject.toml
───────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: pyproject.toml
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ [tool.pyright]
   2   │ typeCheckingMode = "normal"
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

~/work/hypothesis via 🐍 v3.11.10 (python-3.11)
❯ pyright .
Config "typeCheckingMode" entry must contain "off", "basic", "standard", or "strict".
/Users/derekwan/work/hypothesis/foo.py
  /Users/derekwan/work/hypothesis/foo.py:3:13 - information: Type of "booleans().filter(lambda n: n)" is "SearchStrategy[bool]"
0 errors, 0 warnings, 1 information

Now in strict mode:

~/work/hypothesis via 🐍 v3.11.10 (python-3.11)
❯ cat pyproject.toml
───────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: pyproject.toml
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ [tool.pyright]
   2   │ typeCheckingMode = "strict"
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

~/work/hypothesis via 🐍 v3.11.10 (python-3.11)
❯ pyright .
/Users/derekwan/work/hypothesis/foo.py
  /Users/derekwan/work/hypothesis/foo.py:3:13 - error: Type of "filter" is partially unknown
    Type of "filter" is "(condition: (Unknown) -> object) -> SearchStrategy[bool]" (reportUnknownMemberType)
  /Users/derekwan/work/hypothesis/foo.py:3:13 - information: Type of "booleans().filter(lambda n: n)" is "SearchStrategy[bool]"
  /Users/derekwan/work/hypothesis/foo.py:3:38 - error: Type of parameter "n" is unknown (reportUnknownLambdaType)
  /Users/derekwan/work/hypothesis/foo.py:3:41 - error: Return type of lambda is unknown (reportUnknownLambdaType)
3 errors, 0 warnings, 1 information

@tybug
Copy link
Member

tybug commented Feb 17, 2025

Thanks. I still can't reproduce:

 ~/Desktop/temp λ pyright --verbose '/Users/tybug/Desktop/temp/main.py'
Loading pyproject.toml file at /Users/tybug/Desktop/temp/pyproject.toml
Auto-excluding **/node_modules
Auto-excluding **/__pycache__
Auto-excluding **/.*
Assuming Python version 3.13.1.final.0
Execution environment: python
  Extra paths:
    (none)
  Python version: 3.13.1.final.0
  Python platform: Darwin
  Search paths:
    /opt/homebrew/lib/python3.11/site-packages/pyright/dist/dist/typeshed-fallback/stdlib
    /Users/tybug/Desktop/temp
    /Users/tybug/Desktop/temp/typings
    /opt/homebrew/lib/python3.11/site-packages/pyright/dist/dist/typeshed-fallback/stubs/...
    /opt/homebrew/Cellar/[email protected]/3.13.1/Frameworks/Python.framework/Versions/3.13/lib/python3.13
    /opt/homebrew/Cellar/[email protected]/3.13.1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/lib-dynload
    /opt/homebrew/lib/python3.13/site-packages
    /opt/homebrew/Cellar/openvino/2024.6.0_1/libexec/lib/python3.13/site-packages
Found 1 source file
pyright 1.1.394
/Users/tybug/Desktop/temp/main.py
  /Users/tybug/Desktop/temp/main.py:3:13 - information: Type of "booleans().filter(lambda n: n)" is "SearchStrategy[bool]"
0 errors, 0 warnings, 1 information 
Completed in 0.31sec
 ~/Desktop/temp λ cat pyproject.toml 
[tool.pyright]
typeCheckingMode = "strict"
 ~/Desktop/temp λ cat main.py 
from hypothesis.strategies import booleans

reveal_type(booleans().filter(lambda n: n))
 ~/Desktop/temp λ python3
Python 3.12.3 (main, Apr  9 2024, 08:09:14) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import hypothesis
>>> hypothesis.__version__
'6.125.3'
>>> ^D
 ~/Desktop/temp λ 

I also tried pythonVersion = "3.11" in pyproject.toml to match your environment, but that didn't reproduce either.

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