Repository navigation
add universaldetector - #3734
Conversation
srittau
left a comment
There was a problem hiding this comment.
Thank you! A few remarks below.
__init__.pyi should look like this (minus the explanatory comments):
# The following line will re-export UniversalDetector like the implementation does.
# The "as ..." part is required for re-export in stubs.
from .universaldetector import UniversalDetector as UniversalDetector
# The next line will mark this stub as incomplete.
def __getattr__(name: str) -> Any: ... # incomplete|
|
||
| class UniversalDetector: | ||
| if sys.version_info >= (3, 8): | ||
| from typing import TypedDict |
There was a problem hiding this comment.
While TypedDict is only available in typing starting in Python 3.8, you can just import it from typing_extensions for earlier Python versions:
if sys.version_info >= (3, 8):
from typing import TypedDict
else:
from typing_extensions import TypedDictType checkers support this. This way you would avoid the version checks below and make the typed dicts work with all Python versions.
There was a problem hiding this comment.
Even easier, you can also just do from typing_extensions import TypedDict so you don't need the version check.
There was a problem hiding this comment.
Thanks for that both of you! Sotyping_extensions has the required info anyway?
There was a problem hiding this comment.
@JelleZijlstra Stubs of some other modules seems to be doing the version check as @srittau mentioned. As in
if sys.version_info >= (3, 8):
from typing import Final
else:
from typing_extensions import Final
from stdlib/3/dbm/dumb.pyi
Does this method have any advantage?
There was a problem hiding this comment.
No advantage, except that whenever 3.7 support ends, a simple grep for "version_info" will flag this. But we should just grep for typing_extensions when that happens.
There was a problem hiding this comment.
You mean when typing_extensions stop supporting 3.7?
There was a problem hiding this comment.
No, when typeshed stops supporting 3.7 in a few years, we can just do from typing import Final in typeshed.
srittau
left a comment
There was a problem hiding this comment.
Looks good, just two more suggestions.
| if sys.version_info >= (3, 8): | ||
| from typing import TypedDict | ||
|
|
||
| class FinalResultType(TypedDict): |
There was a problem hiding this comment.
Since this is a typestub-only type, it should start with an underscore.
| confidence: float | ||
| language: str | ||
|
|
||
| class IntermediateResultType(TypedDict): |
There was a problem hiding this comment.
And I had a doubt. If I have a set of files all of which use a specific TypedDict class for typing. Should I include the TypedDict class in all these files or is there a way I can define it in one place and import it into those files?
There was a problem hiding this comment.
You can just import it from another file.
There was a problem hiding this comment.
Just like python modules? Cool.
There was a problem hiding this comment.
@JelleZijlstra Are there any conventions to be followed for the.pyifile where the TypedDict class is defined? The definition which can then be imported to other files?
There was a problem hiding this comment.
The name should probably be prefixed with _ to indicate that it doesn't exist at runtime.
There was a problem hiding this comment.
Got another doubt. Can you help me out?
I came across a line like this:
if sys.version_info < (3, 0):
base_str = (str, unicode)
else:
base_str = (bytes, str)
What should be the type of base_str? I figure it starts with something like Tuple[Type] but couldn't be sure. What do you think?
There was a problem hiding this comment.
@JelleZijlstra I think I found another way to keep the common class definition in one place. Just defined it inside the __init__.pyi file. Can then load with from . import <type>.
Started adding files for chardet. Hopefully others will hop in to finish.