-
Notifications
You must be signed in to change notification settings - Fork 767
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
Names of positional-only arguments should be allowed to mismatch #1017
Comments
Thanks for the bug report. This will be addressed in the next release. |
This issue has been fixed in version 2021.3.1, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202131-10-march-2021 |
I think we could have a regression here :( Environment data
Sample codeclass Foo(Bar):
@overload
def find_item(self, product_id: int, name: str) -> requests.Response:
"""Finds item by its name and the product id (UQ)"""
@overload
def find_item(self, id: int) -> requests.Response:
"""Finds item by its id"""
def find_item(self, id_or_product_id: int, name: str = None) -> requests.Response:
data = {
'id': id_or_product_id if not name else None,
'product_id': id_or_product_id if name else None,
'name': name
}
return requests.get(self.url, data=data) Actual behavior
Expected behaviorThe naming should be considered ok. RemarksCalls to Foo.find_item would be either: foo = Foo()
foo.find_item(60, "my_item") # Find by product ID and name
foo.find_item(183617) # Find by id One could argue that the overloaded signatures are overlapping and it is massively easy to make a mistake there but the issue still exists in the following example: class Foo(Bar):
@overload
def find_item(self, name: str, product_id: int) -> requests.Response:
"""Finds item by its name and the product id (UQ)"""
@overload
def find_item(self, id: int) -> requests.Response:
"""Finds item by its id"""
def find_item(self, name_or_id: Union[int, str], product_id: int = None) -> requests.Response:
data = {
'id': name_or_id if isinstance(name_or_id, int) else None,
'name': name_or_id if isinstance(name_or_id, str) else None,
'product_id': product_id
}
return requests.get(self.url, data=data) Complaining about:
My type checking mode is set to basic when facing this issue. |
Thanks. This issue is closed. I'd appreciate it if you'd open a new one and provide a minimal, self-contained code sample - one that doesn't depend on classes and types that are not declared in the sample. |
@Wayofthesin, I think I understand what you're trying to do in your example. Pylance is correct in reporting a bug here. The problem is that the # This will crash
Foo().find_item(id=3) If you want to specify that @overload
def find_item(self, product_id: int, /, name: str) -> requests.Response:
...
@overload
def find_item(self, id: int, /) -> requests.Response:
...
def find_item(self, id_or_product_id: int, /, name: str = None) -> requests.Response:
... Pylance will no longer emit an error if you make this correction to your code. |
Yeah I just realized it. Forget the ticket please (facepalm) |
Environment data
Sample code
Actual behavior
A warning is emitted.
Expected behavior
The naming in sample code should be considered OK.
Calls to
foo
would be like eitherfoo(Date(2021, 3, 5))
orfoo(2021, 3, 5)
, and argument names would never get used outside the function implementation. So there would be no conflicts between the mismatching names.The text was updated successfully, but these errors were encountered: