Instagram / MonkeyType

A Python library that generates static type annotations by collecting runtime types
Other
4.72k stars 170 forks source link

pep_563 flag creates imports that don't match the usage of the name #313

Open sternj opened 9 months ago

sternj commented 9 months ago

Running monkeytype run NumPyCNN.py; monkeytype apply extracted_conv_ on this gist produces irregular imports, which look like this:

def extracted_conv_(filter_size: int, img: from typing import TYPE_CHECKING

numpy.ndarray,conv_filter: from typing import TYPE_CHECKING

numpy.ndarray,result: from typing import TYPE_CHECKING

numpy.ndarray) -> from typing import TYPE_CHECKING

numpy.ndarray :
carljm commented 8 months ago

I'm not able to reproduce the results you show. Using Python 3.12 and the latest MonkeyType and the code in your gist (after pip install scikit-image), I ran monkeytype run NumPyCNN.py and then monkeytype apply --pep_563 extracted_conv_, after which the first few lines of extracted_conv_.py look like this:

from __future__ import annotations
import skimage.data
import sys
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from numpy import ndarray

def extracted_conv_(filter_size: int, img: numpy.ndarray,conv_filter: numpy.ndarray,result: numpy.ndarray) -> numpy.ndarray:

This is buggy (because the annotations use numpy.ndarray while the import is from numpy import ndarray), but it doesn't look quite as buggy as your report shows.

I'll look into fixing the bug I observe; let me know if you have any further theories about what I should try to reproduce your results. It might be related to the LibCST version; I'm using libcst==1.1.0.

sternj commented 8 months ago

Confirmed that working with the current release of Monkeytype and libcst==1.1.0 everything's fine on this issue, do you have a tracking issue for the other bug that you observed?

carljm commented 8 months ago

I don't, I'm inclined to just reopen this issue and use it, since it's the same repro case (albeit different output.)

When you say "everything's fine on this issue," do you mean that you observe the same output I did (which is not exactly "fine" but it isn't as bad as what you got), or do you mean that you actually observe fully-working output?

sternj commented 8 months ago

I meant that I'm observing the same output as you do, I'm also running into fun issues with ellipses not having qualnames in the serialization phase that I'm trying to get a replication of for you, I have a fork that adds in a branch to account for it but it's a hacky fix for a problem that I can't replicate on another computer.