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 guard in list comprehension with assignment expression does not constrain type #8236

Closed
fluggo opened this issue Jan 3, 2020 · 4 comments
Labels
topic-pep-572 PEP 572 (walrus operator)

Comments

@fluggo
Copy link

fluggo commented Jan 3, 2020

  • Are you reporting a bug, or opening a feature request? Bug
from typing import List, Iterator
import os
import re
    
def get_schema_versions(self) -> List[int]:
    return [int(match.group(0))
        for filename in os.listdir('schema')
        if (match := re.fullmatch(r'(\d+)\.sql', filename)) is not None]

def get_schema_versions2(self) -> Iterator[int]:
    for filename in os.listdir('schema'):
        match = re.fullmatch(r'(\d+)\.sql', filename)

        if match is not None:
            yield int(match.group(0))
  • What is the actual behavior/output?

In the first function, mypy interprets the match variable as Optional[Match[str]] and flags the use of the possibly-None object. In the second function, match under the if statement is considered Match[str].

  • What is the behavior/output you expect?

Since both statements guard the match variable against being None, both uses should be of type Match[str].

  • What are the versions of mypy and Python you are using? mypy 0.761, Python 3.8
    Do you see the same issue after installing mypy from Git master? Don't know how
  • What are the mypy flags you are using? (For example --strict-optional) Nothing relevant
@JelleZijlstra JelleZijlstra added the topic-pep-572 PEP 572 (walrus operator) label Jan 6, 2020
@fluggo
Copy link
Author

fluggo commented Jan 6, 2020

This might be the same bug as #7316.

hauntsaninja pushed a commit to hauntsaninja/mypy that referenced this issue Jan 9, 2020
In theory, this draft commit fixes python#8236

However, it looks like the ComparisonExpr part of
find_isinstance_check_helper was rewritten today morning in python#8151.
It looks like it's not super easy to merge: `is_valid_target` gets in the
way and I'm a lot less sure about the change.

I'm also not fully sure about the implications of making
AssignmentExpr into mypy.literals, but seems like it's what we'd want.

I also don't really like the way I've branched for AssignmentExpr, it's
pretty unsatisfactory. Well, work in progress!
hauntsaninja pushed a commit to hauntsaninja/mypy that referenced this issue Jan 9, 2020
In theory, this draft commit fixes python#8236

However, it looks like the ComparisonExpr part of
find_isinstance_check_helper was rewritten today morning in python#8151.
It looks like it's not super easy to merge: `is_valid_target` gets in the
way and I'm a lot less sure about the change.

I'm also not fully sure about the implications of making
AssignmentExpr into mypy.literals, but seems like it's what we'd want.

I also don't really like the way I've branched for AssignmentExpr, it's
pretty unsatisfactory. Well, work in progress!
@msullivan
Copy link
Collaborator

Closing in favor its duplicate #8447 which is about to be fixed anyway

@hauntsaninja
Copy link
Collaborator

@fluggo I was wondering if the code you're working on is open source? My interest is in adding projects to https://github.com/hauntsaninja/mypy_primer that use newer Python features.

@fluggo
Copy link
Author

fluggo commented Sep 28, 2020

@hauntsaninja Sorry, this project wasn't. I appreciate the ask, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-pep-572 PEP 572 (walrus operator)
Projects
None yet
Development

No branches or pull requests

4 participants