I'm trying to correctly annotate types in a function operating on python packages. Empirically, I know that a package has __path__ attribute. At runtime, both package (a folder with __init__.py) and a module (a .py file) have type of <class 'module'>, which can be annotated by types.ModuleType; however a module has no __path__ attribute.
Demo using built-ins:
import asyncio
import turtle
print(type(asyncio)) # <class 'module'>
print(type(turtle)) # <class 'module'>
print(asyncio.__path__) # [.../lib/python3.7/asyncio']
print(turtle.__path__) # AttributeError: module 'turtle' has no attribute '__path__'
What is the type of Package and how to annotate it?
Example code:
import types
def foo(x: types.ModuleType) -> str:
return x.__path__
fails upon mypy check with error: Module has no attribute "__path__". Which type should I use to say that only packages, and not modules, should be passed to my function?