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 is not narrowed in walrus conditional #474

Closed
layday opened this issue Jan 6, 2020 · 3 comments
Closed

Type is not narrowed in walrus conditional #474

layday opened this issue Jan 6, 2020 · 3 comments
Labels
addressed in next version Issue is fixed and will appear in next published version enhancement request New feature or request

Comments

@layday
Copy link

layday commented Jan 6, 2020

Describe the bug
This is another issue that was raised on the mypy tracker that also affects Pyright. Conditionals of assignment expressions are not narrowed for type, producing errors like this one:

{
	"resource": "test.py",
	"owner": "_generated_diagnostic_collection_name_#0",
	"severity": 8,
	"message": "'foo' is not a known member of 'None'",
	"source": "pyright (reportOptionalMemberAccess)",
	"startLineNumber": 16,
	"startColumn": 11,
	"endLineNumber": 16,
	"endColumn": 14
}

To Reproduce
Type check the snippet below.

Expected behavior
That a would not be None under the bad conditional.

Screenshots or Code

# pyright: strict

from typing import Optional

class C:
    def foo(self):
        pass

def good(b: Optional[C]) -> None:
    a = b
    if a:
        a.foo()

def bad(b: Optional[C]) -> None:
    if a := b:
        a.foo()

VS Code extension or command-line
extension, 1.1.17

Additional context
See python/mypy#7316.

@erictraut erictraut added the enhancement request New feature or request label Jan 6, 2020
@erictraut
Copy link
Collaborator

Thanks for the bug report. This will be addressed in the next version of Pyright.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Jan 9, 2020
@erictraut
Copy link
Collaborator

This support is now included in 1.1.19, which I just published.

@alexkoay
Copy link

There might be a regression with this.

import re
def foo(s: str) -> str:
	if m := re.fullmatch('(test).+', s):
		return m.group(1)
	return 'oops'

group reports a reportOptionalMemberAccess error under Pyright 1.1.38.

heejaechang added a commit to heejaechang/pyright that referenced this issue Nov 3, 2021
…ll (microsoft#474)

* made _handleMemoryHighUsage to drop parse tree and binding info as well

* applied Eric's changes

* undo some stuff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version enhancement request New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants