8000 Prime number sieve with generators example does not pass mypy · Issue #17414 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Prime number sieve with generators example does not pass mypy #17414

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 “ 8000 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

Closed
chrisirhc opened this issue Jun 21, 2024 · 2 comments
Closed

Prime number sieve with generators example does not pass mypy #17414

chrisirhc opened this issue Jun 21, 2024 · 2 comments
Labels
bug mypy got something wrong

Comments

@chrisirhc
Copy link
chrisirhc commented Jun 21, 2024

Bug Report

The Prime number sieve with generators Mypy with static typing example does not pass on mypy.
It's also not clear what needs to be done to make the example work. Typing numbers seems to work.

To Reproduce

# Taken directly from https://mypy-lang.org/examples.html
import itertools
from typing import Iterator

def iter_primes() -> Iterator[int]:
     # An iterator of all numbers between 2 and
     # +infinity
     numbers = itertools.count(2)

     # Generate primes forever
     while True:
         # Get the first number from the iterator
         # (always a prime)
         prime = next(numbers)
         yield prime

         # This code iteratively builds up a chain
         # of filters...
         numbers = filter(prime.__rmod__, numbers)

for p in iter_primes():
    if p > 1000:
        break
    print(p)

https://mypy-play.net/?mypy=master&python=3.9&gist=52970ddb2c5cd827dafa8f8af5219f89

Expected Behavior

No errors.

Actual Behavior

main.py:18: error: Incompatible types in assignment (expression has type "filter[int]", variable has type "count[int]")  [assignment]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy master, mypy 1.10.0 (I tried all the versions on the playground on https://mypy-play.net/, but none seem to work. I think it worked with 0.750 but I haven't tested it to be sure.)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.12, 3.9

Suggested fix
I guess changing numbers = itertools.count(2) to numbers: Iterator[int] = itertools.count(2) seems to work. Perhaps we should update the mypy example?

@chrisirhc chrisirhc added the bug mypy got something wrong label Jun 21, 2024
chrisirhc added a commit to chrisirhc/bazel-mypy-integration that referenced this issue Jun 21, 2024
@hauntsaninja
Copy link
Collaborator

Yeah, this was broken by a typeshed change a few years ago python/typeshed#5145
In general, mypy-lang.org could use some love. I believe https://github.com/JukkaL/mypy-website is the repo for the website

chrisirhc added a commit to chrisirhc/mypy-website that referenced this issue Jun 30, 2024
The numbers variable needs to be typed with Iteractor[int] to fix a typing error from the change in python/typeshed#5145 .

See python/mypy#17414 and python/typeshed#5145 .
@chrisirhc
Copy link
Author
chrisirhc commented Jun 30, 2024

Ah got it. Thanks! 👍
Opened JukkaL/mypy-website#27

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

No branches or pull requests

2 participants
0