add py.typed file for PEP-561 compatibility #2858
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PEP-561 is the standard for adding typehints to packages.
As flair itself has already type hints for most methods, simply adding a py.typed file to the package data.
To explain further of why PEP-561 is useful:
let's create an environment installing
flair==0.11.3
andmypy
.And create a file
example_flair.py
with the following content:Note that this is clearly a violation as
Sentence
doesn't implement__setitem__
anddata[0]
would be rather aToken
instead of astr
running
mypy example_flair.py
lead to the following error:A user of flair can handle this by adding
type: ignore
to the import, or add the following to the mypy config:either way running
mypy example_flair.py
would pass by ignoring all potential errors:Success: no issues found in 1 source file
However, if we install flair locally, we get the errors we would expect from our incorrect script:
Simply having this
py.typed
file within the package data is enough to provide the users the full mypy warnings for any missusages.(Note that we have set
include_package_data=True
, therefore thepy.typed
file will be automatically added to the package data without further describing it.