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

Erroneous flagging of parentheses in "with" statement in Python 3.9 and below; valid parentheses are flagged #999

Closed
rben01 opened this issue Feb 27, 2021 · 10 comments
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@rben01
Copy link

rben01 commented Feb 27, 2021

Environment data

  • Language Server version: Pylance 2021.2.4
  • OS and version: macOS Big Sur
  • Python version: 3.8.7 from nix (nix-env -iA nixpkgs.python38Full) with an environment created by Poetry

Code Snippet

from pathlib import Path
with (Path(".") / "file.txt").open() as f:
    print(f)

Expected behaviour

Nothing; this is valid code in Python 3.8.

Actual behavior

BB8910F7-4720-4685-978A-3C29D4EA4355

Pylance gives the error Parentheses within "with" statement requires Python 3.10 or newer. This is an incorrect interpretation of parenthesized context managers; this parenthesization has always been allowed.

Logs

Where do I find these? I'm not seeing a large amount of info in any output panels.

@rben01 rben01 changed the title Erroneous flagging of parentheses in "with" statement in pre-Python 3.10; valid parentheses are flagged Erroneous flagging of parentheses in "with" statement in Python 3.9 and below; valid parentheses are flagged Feb 28, 2021
@erictraut
Copy link
Contributor

Thanks for the bug report. This will be fixed in the next release.

@erictraut erictraut added bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed triage labels Feb 28, 2021
@jakebailey
Copy link
Member

This issue has been fixed in version 2021.3.0, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202130-3-march-2021

@akrymskiy
Copy link

akrymskiy commented Apr 9, 2021

I am still seeing this in 2021.4.0 with Python 3.9.4

image

The code works and executes correctly:

with (HyperProcess(Telemetry.DO_NOT_SEND_USAGE_DATA_TO_TABLEAU) as hyper_proc,
	Connection(
		hyper_proc.endpoint,
		hyper_path,
		CreateMode.CREATE_IF_NOT_EXISTS,
		{'lc_time': 'en_US', 'date_style': 'YMD'}
	) as hyper_conn):

@jakebailey jakebailey reopened this Apr 9, 2021
@jakebailey jakebailey added needs investigation Could be an issue - needs investigation and removed fixed in next version (main) A fix has been implemented and will appear in an upcoming version labels Apr 9, 2021
@erictraut
Copy link
Contributor

erictraut commented Apr 10, 2021

Thanks for the bug report. This is indeed a bug, but it's different from the previous one. The previous one involves the use of a call expression where the LHS is parenthesized.

Your example involves the use of a parenthesized tuple. According to the documented Python grammar, parenthesized tuples are not allowed in a with statement (in versions of Python prior to 3.9). I based the implementation of Pylance on this documentation, but it appears that the documentation is wrong. I'll adjust the Pylance implementation accordingly.

@erictraut
Copy link
Contributor

Actually, I take that back. This code generates a runtime exception when I run it in versions of Python prior to 3.9, so the documentation is correct. It appears that this restriction was removed in Python 3.9, not in 3.10. (I was mislead by this documentation which indicates that 3.10 removed this limitation.) I'll change the check to allow parentheses for 3.9 as well.

@erictraut
Copy link
Contributor

This will be fixed in the next release of Pylance.

@erictraut erictraut added fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed needs investigation Could be an issue - needs investigation labels Apr 10, 2021
@akrymskiy
Copy link

Wow - thanks for a quick fix! I wish commercial software companies had this kind of turn-around ;)

@jakebailey
Copy link
Member

This issue has been fixed in version 2021.4.1, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202141-14-april-2021

@lromanov
Copy link

Hi, I've encountered the same problem on v2021.8.1:
The code

with (await self._lock_device(device)):

causes the error message from pylance.

@erictraut
Copy link
Contributor

This issue has already been fixed. If you think that you are seeing a related problem, please open a new issue with a self-contained sample that repros the problem. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

5 participants