-
Notifications
You must be signed in to change notification settings - Fork 482
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
Remove most "type: ignore"s (fix #738) #761
Conversation
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## main #761 +/- ##
==========================================
- Coverage 78.54% 78.43% -0.12%
==========================================
Files 81 80 -1
Lines 5080 5160 +80
==========================================
+ Hits 3990 4047 +57
- Misses 1090 1113 +23
☔ View full report in Codecov by Sentry. |
This fixes #738! Once this is committed, we should strongly avoid adding new I believe that almost all the instances of We also have the issue of "color changing objects" - where a member or variable has different types at different stages in the program. (I see this as a minor defect we should address eventually, as it makes everyone's IDE type results a bit confusing if nothing else.) To satisfy the type checking for these, I am using asserts, example It works fairly well and is documentary. I don't believe that any of these will actually trigger in practice, but if they do, the code was certain to crash on the next line. I propose we only use |
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.
This is really great work, I'm far too excited about all of this! 🚀
I haven't approved simply because there are a couple of sections where I needed to defer to @blythed for further inspection (these are tagged in the PR).
To unlock this, we do need to shift a bunch of work into runtime ie assert
statements. Long-term, we will want to remove these and have the code structured so that (hopefully) the type-checker can do all this work at validation time ie no run-time hit. Is this issue tracked somewhere (and could you create it if not).
Great work 💪
@@ -21,6 +21,8 @@ def wrapper( | |||
|
|||
|
|||
class Job: | |||
callable: t.Optional[t.Callable] |
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.
Could we put the type hint on the instance parameter instead, and then we do not need this class variable.
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.
Then our documentation builder doesn't pick it up:
Whoa, is that out of date. :-o
class One:
"""dox"""
def __init__(self, one: str):
self.one = one
@dc.dataclass
class Two:
"""dox"""
two: str
class Three
"""dox"""
three: str
def __init__(self, three str):
self.three = three
In class One
the class member is visible to mypy, but not to our documentation builder, though it does represent the constructor correctly.
In Two, everything is visible.
In Three, everything is visible, but there's duplication in our code.
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.
What about these suggestion solutions?
if self.train_y is not None: | ||
out.append(self.train_y) | ||
return out # type: ignore[return-value] | ||
if isinstance(self.train_y, list): |
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.
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.
The issue is with self.train_X
and self.train_y
which I think are supposed to be strings, but in some code paths are treated as lists of strings? Or... the other way around?
@@ -86,6 +91,29 @@ def evaluating(self): | |||
def train(self): | |||
raise NotImplementedError | |||
|
|||
if t.TYPE_CHECKING: |
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.
@blythed For lines 94-116
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.
This is great to reduce the number of mypy
quibbles, so that
we can actually heed it's warnings. What is the secret sauce here?
I see that assert isinstance...
helps. Is there anything else?
There are some changes you made to fix the disagreement between typing and code. I think that the intention was the other way around. Good spot, however, and the fixes would have been correct under the assumptions you made.
superduperdb/container/model.py
Outdated
@@ -203,11 +203,11 @@ def _predict_with_select( | |||
): | |||
ids = [] | |||
if overwrite: | |||
query = select.select_ids | |||
query = select.select_ids() # TODO: is this right? |
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.
This fix is not correct. (Original is correct.) Was this flagged by mypy
or similar?
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.
Ah, it's an error in a declaration elsewhere, this code is correct as you say!
Fixed the declaration and this code.
superduperdb/container/model.py
Outdated
outputs = [ | ||
self.encoder( | ||
x | ||
).encode() # very very very very asdfasdfasdfasdfasdfasdfasdfasdfa |
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.
Is this a TODO?
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.
URG, hahaha, I needed to break the line into parts so I could exactly find the error, and put the comment in to force ruff not to break it up.
The code is fine.
superduperdb/container/model.py
Outdated
) | ||
results = {} | ||
|
||
# TODO: metrics is definitely not iterable | ||
# TODO: metrics is definitely not iterable: this can't work |
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.
Yes this is an error type should be t.List[Metric]
for r in validation_set.data | ||
], | ||
) | ||
out = m(prediction_X, prediction_y) # type: ignore[index] |
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.
metrics
should be a list.
There was an issue with |
039cb36
to
7542b93
Compare
Mostly the secret sauce was deducing the right types and putting them in. The Occasionally mypy doesn't understand the type of a local variable. Often just reorganizing the expressions makes it work. Sometimes you need to declare the local variable the first time it is seen. In one case I had a add a type annotation and I have no idea why. About the duplication of the declaration of member variables: sphinx doesn't parse the code so it can't guess any type annotations from inside executable code, e.g. the constructor. So without the top-level annotations, the sphinx documentation won't show class members at all. Our trouble is that we have to serve three masters, Python, mypy and sphinx, and sphinx doesn't do anything fancy, it just imports stuff and then pulls the type annotations and doc comments off them. mypy can look into the constructor, see the assignment of Using one of our three possible data classes solves all three problems neatly. Two out of four compilations failed: 3.8 and 3.11. Weirdly, these are the two I test locally! I think it's a glitch but I see too many of these... |
So I need approval! There are a couple more TODOs yet unanswered, but I'll push those at you in a later commit. |
a06052b
to
34faa1f
Compare
cbed66e
to
a499ff7
Compare
80564c4
to
bb98a09
Compare
@blythed Let's get it out!!! |
Description
Related Issues
Checklist
make test
successfully?Additional Notes or Comments