Search code examples
pythonpython-3.xtype-hintingpyc

Do .pyc files contain type hinting information?


I assumed standard library type hinting support was all based on reading the plain source like inspect does for some things, but I looked at typing.py in the standard library and it seems the compiler does store type hint info on the object itself.

So, if I do:

def myfunc(a: int, b:int): -> str
    return "{a} + {b} = {a+b}"

and then compile that to a .pyc file, does the type information stick around such that someone who I've given the .pyc file can use things like typing.get_type_hints() to access it? (With the assumption that I have a valid reason to distribute just the .pyc.)


Solution

  • Well I have just impersonated your someone here :) with your code and the answer is yes it works, after I changed the def myfunc(a: int, b:int): -> str to

    def myfunc(a: int, b:int) -> str:
    

    otherwise it's a syntax error.