pyodide / webtypy

Python type definitions for web APIs
Mozilla Public License 2.0
13 stars 3 forks source link

Names defined in `__init__.pyi` are not the same as names that can be imported from `js` #14

Open hoodmane opened 9 months ago

hoodmane commented 9 months ago

In __init__.py we see stuff like:

class WindowOrWorkerGlobalScope:
    indexedDB: IDBFactory
    ...

So this typechecks but raises an ImportError at runtime:

from js import WindowOrWorkerGlobalScope

And this is a type error but works at runtime:

from js import indexedDB
simonegiacomelli commented 9 months ago

Let's start with the simpler issue, importing a mixin. We can detect that WindowOrWorkerGlobalScope is a mixin from the webidl. A mixin is not a concrete interface so it does not exist like a concrete interface (like Window).

A simple solution could be to rename the mixin with a starting _ (e.g., _WindowOrWorkerGlobalScope) so it will still be part of the type hierarchy.

This will not prevent importing it but it will be a good signal that it is not to be imported. At this moment I have no other ideas.

simonegiacomelli commented 9 months ago

A quick comment about

from js import indexedDB

This is valid because window is a global scope and window has a property called indexDB. I did some quick and dirty to have something working here

Probably the correct thing to do is to detect all the global objects (are there others than window?) and copy all the properties of its corresponding type (Window) at the root of the pyi.

What do you think?

Any feedback on this or the previous comment are welcome!

hoodmane commented 9 months ago

A simple solution could be to rename the mixin with a starting _

This seems to be the officially recommended approach: https://typing.readthedocs.io/en/latest/source/stubs.html#private-definitions

hoodmane commented 9 months ago

Actually I think the better approach is to make a _types.py file with a __all__ field listing all the public stuff and then in __init__.py do:

from ._types import *

This works the way we want: https://typing.readthedocs.io/en/latest/source/stubs.html#imports