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 tracking conditionally extended empty tuple #1359

Closed
Fatsie opened this issue May 27, 2021 · 3 comments
Closed

Type tracking conditionally extended empty tuple #1359

Fatsie opened this issue May 27, 2021 · 3 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

@Fatsie
Copy link

Fatsie commented May 27, 2021

Environment data

  • Language Server version: 2021.5.4
  • OS and version: Ubuntu 20.04
  • Python version (& distribution if applicable, e.g. Anaconda): conda 3.7.10

Reproducer & Actual behaviour

I reduced my problem to following code that can reproduce it:

from typing import Tuple

def f(n: int, check: bool):
    t: Tuple[Tuple[int, float], ...] = tuple()
    for _ in range(n):
        t += ((0, 0.0),)
        if check:
            t += ((1, 1.0), (2, 2.0))

    for (i, f) in t:
        print(i)

This give the following error on the second for line:

{
	"resource": "/home/verhaegs/eda/test/test_emptytuple.py",
	"owner": "_generated_diagnostic_collection_name_#2",
	"code": {
		"value": "reportGeneralTypeIssues",
		"target": {
			"$mid": 1,
			"external": "https://github.com/microsoft/pylance-release/blob/main/DIAGNOSTIC_SEVERITY_RULES.md#diagnostic-severity-rules",
			"path": "/microsoft/pylance-release/blob/main/DIAGNOSTIC_SEVERITY_RULES.md",
			"scheme": "https",
			"authority": "github.com",
			"fragment": "diagnostic-severity-rules"
		}
	},
	"severity": 8,
	"message": "\"object\" is not iterable\n  \"__iter__\" method not defined",
	"source": "Pylance",
	"startLineNumber": 10,
	"startColumn": 9,
	"endLineNumber": 10,
	"endColumn": 15
}

Problem seems related to the fact that on the empty tuple() assignment the type of t is tuple[_T_co@tuple] and not the explicitly specified type. When I move the first t += statement outside the loop all is honky dory.

Expected behaviour

No typing error given.

@erictraut
Copy link
Contributor

Thanks for the bug report. The problem appears to be with the initial assignment of t — an interaction between the tuple() constructor call and the type annotation. I'll investigate further. In the meantime, you can work around the problem by replacing the call to the constructor (tuple()) with an empty tuple expression (()).

@erictraut
Copy link
Contributor

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 needs investigation Could be an issue - needs investigation labels May 27, 2021
@jakebailey
Copy link
Member

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

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

4 participants