Instagram / MonkeyType

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

Standard Example does not work #158

Open noragen opened 4 years ago

noragen commented 4 years ago

Hi @carljm , thanks for the hints! 😊 My setup is following:

(base) C:\projects>conda info

     active environment : base
    active env location : C:\Anaconda3
            shell level : 1
       user config file : C:\Users\tfz\.condarc
 populated config files : C:\Users\tfz\.condarc
          conda version : 4.7.12
    conda-build version : 3.18.11
         python version : 3.7.3.final.0
       virtual packages :
       base environment : C:\Anaconda3  (writable)
           channel URLs : https://conda.anaconda.org/conda-forge/win-64
                          https://conda.anaconda.org/conda-forge/noarch
                          https://repo.anaconda.com/pkgs/main/win-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/win-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/msys2/win-64
                          https://repo.anaconda.com/pkgs/msys2/noarch
                          https://conda.anaconda.org/bioconda/win-64
                          https://conda.anaconda.org/bioconda/noarch
          package cache : C:\Anaconda3\pkgs
                          C:\Users\tfz\.conda\pkgs
                          C:\Users\tfz\AppData\Local\conda\conda\pkgs
       envs directories : C:\Anaconda3\envs
                          C:\Users\tfz\.conda\envs
                          C:\Users\tfz\AppData\Local\conda\conda\envs
               platform : win-64
             user-agent : conda/4.7.12 requests/2.22.0 CPython/3.7.3 Windows/10 Windows/10.0.18362
          administrator : False
             netrc file : None
           offline mode : False

(base) C:\projects>conda list monkeytype
# packages in environment at C:\Anaconda3:
#
# Name                    Version                   Build  Channel
monkeytype                19.11.2            pyh516909a_0    conda-forge

The monkeytype.sqlite3 is there, but seems to be empty. Pls see the attachment. monkeytype.zip

I'm very thankful, that there are so guys like you who responds so fast! Thank you very much in advance! πŸ˜‰

If you need more information or when you could give some debug hints, I really would like to help.

Best regards!

Originally posted by @noragen in https://github.com/Instagram/MonkeyType/issues/157#issuecomment-564929257

carljm commented 4 years ago

Hi @noragen, did I understand correctly in #157 that it works for you with Python 3.8 but not with 3.7.3 as above?

noragen commented 4 years ago

Hi @carljm, actually yes, BUT I've now updated my conda base from 2019.03 to 2019.10 and python 3.7.3 to 3.7.5 And what should I say: The problem still exists in 3.7.5 base environment. So. Then I made a new virtual environment and tried it in the dedicated virtual env again:

(base) C:\projects>conda create -n py375 python=3.7.5 monkeytype
(base) C:\projects>conda activate py375
(py375) C:\projects>monkeytype run myscript.py
(py375) C:\projects>monkeytype stub some.module
def add(a: int, b: int) -> int: ...
(py375) C:\projects>

Interesting. I'm going to try to figure out whats happening here...

The first thing what came to my eyes was following: It is not a quite good idea to use imports of the same filename like the one in the standard-library: grafik

Stay tuned. More is going to follow.

noragen commented 4 years ago

Hi @carljm ,

It looks like, that the CallTracer just logs nothing for any reason...

class CallTraceStoreLogger(CallTraceLogger):
    """A CallTraceLogger that stores logged traces in a CallTraceStore."""
    def __init__(self, store: CallTraceStore) -> None: # βœ” IS CALLED.
        self.store = store
        self.traces: List[CallTrace] = []

    def log(self, trace: CallTrace) -> None: # πŸ’₯ IS NOT CALLED!
        if not trace.func.__module__ == '__main__':
            self.traces.append(trace)

    def flush(self) -> None: # βœ” IS CALLED.
        self.store.add(self.traces)
        self.traces = []

After long trying around I get following: I tried a simple example, looked whether there is a problem to set the profiler. I just executed a simple example and it worked in both environments (base and py375)

import sys

def test(n):
    j = 0
    for i in range(n):
        j = j + i
    return n

def profiler(frame, event, arg):
    print (event, frame.f_code.co_name, frame.f_lineno, "->", arg)

# profiler is activated on the next call, return, or exception
sys.setprofile(profiler)

# profile this function call
test(1)

# disable profiler
sys.setprofile(None)

# don't profile this call
test(2)

which gave me in both environements:

(py375) C:\projects>python test_profile.py
call test 3 -> None
return test 7 -> 1
c_call <module> 19 -> <built-in function setprofile>

(py375) C:\projects>

and

(base) C:\projects>python test_profile.py
call test 3 -> None
return test 7 -> 1
c_call <module> 19 -> <built-in function setprofile>

(base) C:\projects>

So, now is the question, what is the problem, in my base-environment? What is different then, what prevents it from normal execution? The CallTraceStoreLogger.log was not being called.

Maybe it is any ContextManager related weird stuff? Or anything else?

Maybe you have an idea?

Best regards!

carljm commented 4 years ago

Hi @noragen -- really not sure. I recommend you see if CallTracer.__call__ is being called; that's the profiler entry point. If not, then the problem is with installing the profiler. If so, maybe you can trace where things are going off the rails from there.