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

Types not narrowed with constrained TypeVar and walrus #12190

Closed
ghost opened this issue Feb 16, 2022 · 6 comments
Closed

Types not narrowed with constrained TypeVar and walrus #12190

ghost opened this issue Feb 16, 2022 · 6 comments
Labels
bug mypy got something wrong topic-pep-572 PEP 572 (walrus operator) topic-type-narrowing Conditional type narrowing / binder

Comments

@ghost
Copy link

ghost commented Feb 16, 2022

Bug Report

It's not entirely clear to me if the existing issues regarding the walrus operator are about the same issue, feel free to close this if they are.

With the following code:

if m := re.search(pattern, string):
    return m.group(group)

I get this error:

Item "None" of "Optional[Match[Any]]" has no attribute "group"  [union-attr]

Even though it's impossible for m to be None, because we just checked its value in the if.

There are no errors with this code:

m = re.search(pattern, string)
if m:
    return m.group(group)

Your Environment

  • Mypy version used: 0.931, mypy 0.940+dev.777885f2c26cce195f6d23c80fefae1e66dfaac2
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
    python_version = '3.8'
    follow_imports = 'silent'
    ignore_missing_imports = true
  • Python version used: 3.8
  • Operating system and version: Debian 11
@ghost ghost added the bug mypy got something wrong label Feb 16, 2022
@hauntsaninja
Copy link
Collaborator

Hmm, this should work, and I can't repro:
https://mypy-play.net/?mypy=latest&python=3.10&gist=873656796bacd5961b0fd6fd721e83b9

Closing because I'm fairly confident mypy supports this, but feel free to re-open if you have a playground link like^ or a self-contained script that reproduces.

@ghost
Copy link
Author

ghost commented Feb 17, 2022

@hauntsaninja hauntsaninja reopened this Feb 17, 2022
@hauntsaninja hauntsaninja changed the title mypy complains about possible None value when using if with walrus operator Types not narrowed with constrained TypeVar and walrus Feb 17, 2022
@hauntsaninja
Copy link
Collaborator

Thanks!
Note that that overload doesn't actually work... mypy (and other type checkers) only use the overloads to check callers of the function, not the body of the function. The actual body of your function is untyped, so the params are just treated as Any. If you used --strict (which prevents code from going untyped), it would complain about this.

@JelleZijlstra JelleZijlstra added topic-pep-572 PEP 572 (walrus operator) topic-type-narrowing Conditional type narrowing / binder labels Mar 19, 2022
@shartzog
Copy link

shartzog commented May 31, 2023

I have a related issue with mypy==1.3.0, python 3.10.4. Given test_mypy.py containing:

test = ['a', 'b', 'c']
if any((_t := itm) == 'c' for itm in test):
    print(f'{_t=}')
else:
    _t = None
print(_t)

output:

>mypy test_mypy.py
test_mypy.py:3: error: Name "_t" is used before definition  [used-before-def]
Found 1 error in 1 file (checked 1 source file)

Let me know if the problem is unique enough warrant its own issue (or if such an issue already exists) or if it's not a problem for some reason I'm not seeing...

@shartzog
Copy link

shartzog commented Jun 1, 2023

The issue I described doesn't seem to fit with the type-narrowing tag, so I'm opening a new one for this.

@hauntsaninja
Copy link
Collaborator

This issue was fixed in #14135

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-pep-572 PEP 572 (walrus operator) topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

3 participants