pylance forces the import from typehint. #4314
Replies: 10 comments
-
Thanks for the issue but this is by design. It's importing the missing imports when you complete the method. Without those imports it wouldn't run. This was actually an enhancement: |
Beta Was this translation helpful? Give feedback.
-
Thank you for your answer @rchiodo . and, Is this also intended to ignore ex: ## before
from typing import Any, TYPE_CHECKING
from sqlalchemy.types import TypeDecorator
if TYPE_CHECKING:
from sqlalchemy.engine.interfaces import Dialect
from sqlalchemy.sql.type_api import TypeEngine
class Test(TypeDecorator):
def dialect_impl
### after
from typing import Any, TYPE_CHECKING
from sqlalchemy.engine.interfaces import Dialect
from sqlalchemy.sql.type_api import TypeEngine
from sqlalchemy.types import TypeDecorator
if TYPE_CHECKING:
from sqlalchemy.engine.interfaces import Dialect
from sqlalchemy.sql.type_api import TypeEngine
class Test(TypeDecorator):
def dialect_impl(self, dialect: Dialect) -> TypeEngine[Any]:
return super().dialect_impl(dialect) |
Beta Was this translation helpful? Give feedback.
-
There's no way to turn it off. I guess don't use completions for methods would be the way to not have it happen at the moment. The part where it skips TYPE_CHECKING seems like a bug though. It should recognize those imports I believe. |
Beta Was this translation helpful? Give feedback.
-
Thank you for your answer @rchiodo . If this is a bug, would it be better to keep this issue open? |
Beta Was this translation helpful? Give feedback.
-
@phi-friday, why are you placing these imports under a And I'm not convinced that pylance should be be adding auto-imports to import statements that are located within conditional blocks. |
Beta Was this translation helpful? Give feedback.
-
You can close the issue or not based on if you're satisfied with the results. We'll also do the same after the rest of the Pylance team gets a chance to look at it. I think the TYPE_CHECKING part is a bug, so we should keep it open. We may also add a setting to turn off this auto completion, but that would depend upon more people asking for the same thing. |
Beta Was this translation helpful? Give feedback.
-
I don't think this is a bug. Auto-imports shouldn't be added to import statements in conditional blocks because those import statements may not be executed, which will result in a runtime exception when the symbol is later referenced. I suppose it would be possible to determine whether pylance is introducing a use of the symbol only for purposes of a type annotation and the use of that type annotation happens to be deferred (e.g. the file contains a So I think the current behavior is "by design", and this is a feature request. |
Beta Was this translation helpful? Give feedback.
-
When I define I think that's definitely the special case you mentioned. |
Beta Was this translation helpful? Give feedback.
-
Moving this issue to discussion as an enhancement request for comments and upvotes. |
Beta Was this translation helpful? Give feedback.
-
It would be nice to be able to turn off the automatic additions of import statements. Reasoning: I often use to accept the autocompletion, knowing that I will be changing the output type. For instance
Hitting here will autocomplete the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Environment data
Expected behavior
Actual behavior
Logs
in
2023.4.40
in
2023.4.30
more
I downgraded to 2023.4.30 and it worked fine.
"python.analysis.autoImportCompletions": false
has already been added to the setting.Beta Was this translation helpful? Give feedback.
All reactions