microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.71k stars 765 forks source link

When function with `Optional` param is moved, `Optional` is not imported in target file #5553

Open debonte opened 8 months ago

debonte commented 8 months ago

Environment data

Code Snippet

# test.py
from typing import Optional

class Dependency:
    pass

def func(self, param1: Optional[Dependency]):
    pass

Repro Steps

  1. Use the "Move symbol to..." code action to move func to another file.

Expected behavior

from typing import Optional
from test import Dependency

def func(self, param1: Optional[Dependency]):
    pass

Actual behavior

from test import Dependency

def func(self, param1: Optional[Dependency]):
    pass
heejaechang commented 8 months ago

my guess is a bug in pep604 handling in import adder is causing it to skip adding Optional thinking Dependency | None will be used instead of Optional