-
-
Notifications
You must be signed in to change notification settings - Fork 201
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
feat: trait typing #818
feat: trait typing #818
Conversation
Co-authored-by: Steven Silvester <steven.silvester@ieee.org> Co-authored-by: Vidar Tonaas Fauske <510760+vidartf@users.noreply.github.com>
for more information, see https://pre-commit.ci
This makes use of Self, which only seems to work with the current mypy main branch.
Failures are expected due to not being on master (where this would be green). Also, would require a bit more work to get all the other types supported. As @rmorshea said in #788 (comment) there is no way but to copy-paste the |
I'd say partial support is a good start, we can follow up with the rest. |
I'd like to add support to a few more, and see if we can do without the |
cd7c764
to
48f8580
Compare
My guess is that the windows failures are solved by our good friend @vidartf who is always ahead of what we need for windows: davidfritzsche/pytest-mypy-testing#34 |
cc @rmorshea Btw, I've moved away from overloading class Float(TraitType[G, S]):
"""A float trait."""
default_value = 0.0
info_text = "a float"
@t.overload
def __init__(
self: "Float[float, t.Union[int, float]]",
default_value: t.Union[float, Sentinel] = ...,
allow_none: Literal[False] = ...,
**kwargs: t.Any,
):
...
@t.overload
def __init__(
self: "Float[t.Optional[int], t.Union[int, float, None]]",
default_value: t.Union[float, Sentinel, None] = ...,
allow_none: Literal[True] = ...,
**kwargs: t.Any,
):
...
def __init__(self, default_value=Undefined, allow_none=False, **kwargs):
self.min = kwargs.pop("min", -float("inf"))
self.max = kwargs.pop("max", float("inf"))
super().__init__(default_value=default_value, allow_none=allow_none, **kwargs) This looks way less scary than what we suggested in #788 |
@@ -25,7 +25,7 @@ class App(Application): | |||
) | |||
|
|||
def start(self): | |||
print(f"key={self.key}") | |||
print(f"key={self.key.decode('utf8')}") |
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.
Typing is already paying off
9ddce25
to
e45e1e1
Compare
Okay, so basically we need to wait for a new release of mypy, and then if there is not a release of davidfritzsche/pytest-mypy-testing#34, skip the typing tests on Windows. |
|
Not to get too off-topic here, but @blink1073 I am also looking at using pytest-mypy-testing in a project but also am seeing that it is out of date. I was thinking about trying instead to use MyPy's own unit testing framework, which is built on pytest, and was curious if you all had considered that as well.
I wasn't sure if anyone was using this outside of MyPy though. |
Interesting, I think it is tricky to get typing right without testing both successes and failures. |
@maartenbreddels Yes I agree. I wasn't sure if you were implying that the mypy unit testing framework doesn't support testing failures as well, but here is a simple example of using it verify some things that pass and some that fail: https://github.com/python/mypy/blob/54635dec2379e2ac8b65b6ef07778015c69cfb6a/test-data/unit/check-basic.test#L93-L99 |
I just had a go at trying to use MyPy's builtin testing framework in my package, and it seems to work OK! Only requires one monkeypatch... You can see the test file here which verifies that one way of calling a method passes MyPy and the other raises an error: https://github.com/metadsl/egg-smol-python/blob/db300cba4413f2b91cd0b8c3c9ae9cecab089ad1/test-data/unit/check-basic.test |
I wonder if the |
All green, let's gooo! |
Took #788 and vidartf#1 did some fixes for py < 3.10 and squashed it to make rebasing to master easier.
Closes #788