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

Spurious used-before-def in a specific situation using walrus #14683

Closed
tzaffi opened this issue Feb 12, 2023 · 2 comments
Closed

Spurious used-before-def in a specific situation using walrus #14683

tzaffi opened this issue Feb 12, 2023 · 2 comments
Labels
bug mypy got something wrong topic-pep-572 PEP 572 (walrus operator) topic-possibly-undefined possibly-undefined error code

Comments

@tzaffi
Copy link

tzaffi commented Feb 12, 2023

Bug Report

I'm getting the following mypy error used-before-def on the code snippet below:

I played around a bit trying to simplify, but there could be other scenarios I haven't explored.

Generalizing what I deem the problem to be:

WHEN:

  • A function is defined whereby
  • it calls a callable object
  • AND inside the call there an assignment to a new variable
  • AND the call returns another callable object
  • AND the other callable object is supplied the new variable defined in the previous call

THEN:

the used-before-def error is generated even though there is nothing at all incorrect in this scenario since the new variable would ALWAYS be available.

To Reproduce

run mypy version 1.0 on the following:

from typing import Any


class Reporter:
    def __init__(self, x: Any):
        self.x: Any = x

    def report(self, y: Any = None):
        print(self.x)
        if y is not None:
            print(y)


def foo(z: Any):
    Reporter(x := z).report(x)


for i in range(10):
    foo(i)

Expected Behavior

No error should be generated.

Actual Behavior

pyteal/walrus.py:15 error: Name "x" is used before definition  [used-before-def]

Your Environment

Python 3.11.1 running on mac Ventura 13.1

  • Mypy version used: 1.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): not relevant IMO
  • Python version used: 3.11.1
@tzaffi tzaffi added the bug mypy got something wrong label Feb 12, 2023
@AlexWaygood AlexWaygood added topic-pep-572 PEP 572 (walrus operator) topic-possibly-undefined possibly-undefined error code labels Feb 12, 2023
@AlexWaygood
Copy link
Member

Duplicate of #14626

@AlexWaygood AlexWaygood marked this as a duplicate of #14626 Feb 12, 2023
@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Feb 12, 2023
@AlexWaygood
Copy link
Member

A patch release with the fix should hopefully be coming soon! :)

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-possibly-undefined possibly-undefined error code
Projects
None yet
Development

No branches or pull requests

2 participants