-
Notifications
You must be signed in to change notification settings - Fork 803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add type hints to wrappers.py #1835
Conversation
# assign the inner dict manually to prevent __setattr__ from firing | ||
super().__setattr__("_d_", d) | ||
|
||
def __contains__(self, key): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this type be _KeyT
instead of object
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kept is as object
to be consistent with the dict.__contains__()
typing, conceptually I think is also not a type error to ask if an object of a wrong type is in the dictionary, is just gonna be false.
I don't understand this. Mypy bundles typeshed, so aren't all these types already available? This seems to work: if TYPE_CHECKING:
from _operator import _SupportsComparison I'm going to go ahead and add a commit here with this simplification, since I have it working here. If you think this is missing something feel free to remove this commit and continue with your version. |
@Caiofcas Also added some minor changes to the casts, to help mypy do a better job of it and have less explicit casts in the code. Let me know what you think of all these changes. |
Hm, thanks for catching this, I did miss that ofc mypy would have it installed if it was giving the error with these types 🙃 .
They seem ok to me.
If you wanna be super strict this cast is technically wrong since the validation that the keys are |
Looks good to me, thanks for the improvements! |
* refactor: add type hints to wrappers.py * use _SupportsComparison type from typeshed * escape imported types in quotes * simplify casts * fixed linter errors --------- Co-authored-by: Miguel Grinberg <miguel.grinberg@gmail.com> (cherry picked from commit 2c79b48)
Thanks! Note that I started #1845 to support using type hints when defining documents. |
* refactor: add type hints to wrappers.py * use _SupportsComparison type from typeshed * escape imported types in quotes * simplify casts * fixed linter errors --------- Co-authored-by: Miguel Grinberg <miguel.grinberg@gmail.com> (cherry picked from commit 2c79b48) Co-authored-by: Caio Fontes <38675540+Caiofcas@users.noreply.github.com>
Adds type hints to
wrappers.py
and starts to add typing toutils.AttrDict
. Continuation of #1533This one is more complex then previous typing MRs, so I'm open to suggestions. The main points are:
The definition of the
SupportsComparison
TypeAlias to ensure that the operators in Range can be compared. This could posibly be avoided by utilizing the typeshed library, but at this point it seems like overkill to add it as a development dependency for only these 4/5 types.The use of
cast
in some instances where I needed to convey information to mypy that can be easily verified by the code validations, but it's not inferred by the type-checking. I feel that is the cleaner solution and feel that the 3 cases where it's used are justifiable.Leave it up to you to decide on these matters due to lacking the context on how the project maintainers feel about these kind of issues.