-
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
Pylance didn't check return type from __new__ #1092
Comments
See also: microsoft/pyright#625 It's my understanding that this is intentional, and you should be getting a type error for that
Which matches our behavior. |
What is your use case for this pattern, as opposed to using a function? |
While it is unusual for a |
I want to making sub function, like this from typing import Optional
from dataclasses import dataclass
@dataclass
class Package:
@dataclass
class Author:
name: str
mail: Optional[str]
name: str
version: Optional[str]
author: Optional[Author]
class package:
class author:
def __new__(cls, *,
name: str,
mail: Optional[str] = None) -> Package.Author:
return Package.Author(name, mail)
def __new__(cls, *,
name: str,
version: Optional[str] = None,
author: Optional[Package.Author] = None) -> Package:
return Package(name, version, author)
def setup(package: Package) -> None: ...
setup(package(
name = "package",
author = package.author(
name = "PASUNX"
)
))
# error:
# Argument of type "author" cannot be assigned to parameter "author" of type "Author | None" in function "__new__"
# Type "author" cannot be assigned to type "Author | None"
# "author" is incompatible with "Author"
# Cannot assign to "None" Pylance (reportGeneralTypeIssues) I know that I can create "author" function but I don't like to This option work fine but I like the first option from typing import Optional
class Package:
class Author:
name: str
mail: Optional[str]
def __init__(self, *,
name: str,
mail: Optional[str] = None):
self.name = name
self.mail = mail
name: str
version: Optional[str]
author: Optional[Author]
def __init__(self, *,
name: str,
version: Optional[str] = None,
author: Optional[Author] = None) -> None:
self.name = name
self.version = version
self.author = author
def setup(package: Package) -> None: ...
setup(Package(
name = "package",
author = Package.Author(
name = "PASUNX"
)
)) any idea or should I use second option ? |
This bug will be fixed in the next release of pylance. Python constructor calls are complicated for a type evaluator because they involve calls to both the Thanks for the bug report! |
This issue has been fixed in version 2021.3.4, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202134-31-march-2021 |
Environment data
Expected behaviour
Actual behaviour
Logs
Will provide if necessary.
The text was updated successfully, but these errors were encountered: