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

Wrong derivation for Type[T] #1468

Closed
Fatsie opened this issue Jun 18, 2021 · 2 comments
Closed

Wrong derivation for Type[T] #1468

Fatsie opened this issue Jun 18, 2021 · 2 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 Jun 18, 2021

I have following minimal non-working example:

from typing import Generator, List, Type, TypeVar, Union

class LI(List[int]):
    pass
class LS(List[str]):
    pass

_iter_type = TypeVar("_iter_type")
class MyList(List[Union[LI, LS]]):
    def __iter_type__(self, *, type_: Type[_iter_type]) -> Generator[_iter_type, None, None]:
        for elem in self:
            if isinstance(elem, type_):
                yield elem

l = MyList()
def same(other: Union[LI, LS]):
    for elem in l.__iter_type__(type_=other.__class__):
        for v in elem:
            print(v)

The problem seems to be that the derivation of type_ argument for __iter_type__() on line 17 seems to be wrong; pop-up says type_ is Type[Type[LI] | Type[LS]] and it should be Type[LI] | Type[LS]. This seems to be a regression as it did not give errors before last update of vscode.
This gives the following errors:

{
	"resource": "/home/verhaegs/eda/code/PDKMaster/pdkmaster/test_iter.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": "\"Type[LI]\" is not iterable\n  \"__iter__\" method not defined",
	"source": "Pylance",
	"startLineNumber": 18,
	"startColumn": 18,
	"endLineNumber": 18,
	"endColumn": 22
}
{
	"resource": "/home/verhaegs/eda/code/PDKMaster/pdkmaster/test_iter.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": "\"Type[LS]\" is not iterable\n  \"__iter__\" method not defined",
	"source": "Pylance",
	"startLineNumber": 18,
	"startColumn": 18,
	"endLineNumber": 18,
	"endColumn": 22
}

Software used:

  • Pylance 2021.6.2
  • vscode: latest flatpak version 1.56.2-1620837933
  • OS: Ubuntu 20.04.2 LTS
  • Python: anaconda 3.7.10
@erictraut
Copy link
Contributor

Thanks for the bug report. This will be fixed in the next release. I've added a simplified version of your code to our unit tests since it covers a case that was previously not included in our testing.

@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 Jun 18, 2021
@bschnurr
Copy link
Member

This issue has been fixed in version 2021.6.3, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202163-23-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

3 participants