-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
mypy: enable option strict_optional
for 'pylib' (#3317)
#3355
base: main
Are you sure you want to change the base?
mypy: enable option strict_optional
for 'pylib' (#3317)
#3355
Conversation
strict_optional
for pylib
(#3317)strict_optional
for pylib
(#3317)
|
strict_optional
for pylib
(#3317)strict_optional
for 'pylib' (#3317)
f842f5b
to
df33a62
Compare
diff --git a/pylib/anki/models.py b/pylib/anki/models.py
index 0d7024933..d3dd5c886 100644
--- a/pylib/anki/models.py
+++ b/pylib/anki/models.py
@@ -134,9 +134,7 @@ class ModelManager(DeprecatedNamesMixin):
def current(self, for_deck: bool = True) -> NotetypeDict:
"Get current model. In new code, prefer col.defaults_for_adding()"
- model_id = self.col.decks.current().get("mid")
- assert model_id is not None
- notetype = self.get(model_id)
+ notetype = self.get(self.col.decks.current().get("mid"))
if not for_deck or not notetype:
notetype = self.get(self.col.conf["curModel"])
if notetype:
@@ -154,7 +152,7 @@ class ModelManager(DeprecatedNamesMixin):
except NotFoundError:
return None
- def get(self, id: NotetypeId) -> NotetypeDict | None:
+ def get(self, id: NotetypeId | None) -> NotetypeDict | None:
"Get model with ID, or None."
# deal with various legacy input types
if id is None:
diff --git a/pylib/anki/tags.py b/pylib/anki/tags.py
index af1f2c6f9..ce6335991 100644
--- a/pylib/anki/tags.py
+++ b/pylib/anki/tags.py
@@ -121,10 +121,9 @@ class TagManager(DeprecatedNamesMixin):
def rem_from_str(self, deltags: str, tags: str) -> str:
"Delete tags if they exist."
- def wildcard(pat: str, repl: str) -> Match:
+ def wildcard(pat: str, repl: str) -> Match | None:
pat = re.escape(pat).replace("\\*", ".*")
return_value = re.match(f"^{pat}$", repl, re.IGNORECASE)
- assert return_value is not None
return return_value
current_tags = self.split(tags) |
@dae Thanks a lot for the super helpful fix. 🙂 Do these error messages make sense to you?
It seems from aqt.rs that when the following three files are translated to Python code, there's no
|
Making the code generator insert them would be tricky. I suggest diff --git a/.mypy.ini b/.mypy.ini
index 96dae8669..f15a768c2 100644
--- a/.mypy.ini
+++ b/.mypy.ini
@@ -26,6 +26,8 @@ disallow_untyped_defs = True
strict_optional = true
[mypy-aqt.*]
strict_optional = false
+[mypy-_aqt.*]
+strict_optional = false
[mypy-anki.importing.*]
disallow_untyped_defs = False
[mypy-anki.exporting] |
Please note that Anki currently fails to start, because you've added an assert in a location where the value can be none. We'll need to do a quick pass over each of the asserts that have been added to double-check the values should never be None, as there may be other instances where the types were not correctly indicating that the value was nullable. |
Good idea. Thanks for helping me out. 👍
Absolutely. I have no idea if any of the assertions I added are actually correct. I don't understand the codebase well enough to know that. My plan was to first (with the least amount of effort) make all mypy errors disappear, then to make the pipeline succeed if necessary, and then go over all assertions I added and see if they're actually correct. If any assertion is wrong, replace it with a real solution and repeat the step of making the pipeline succeed. Kind of like an "assertion-driven development". 😅 So with your help, I got the pipeline to succeed now. 🙂 Now we can look which assertions need to be replaced. |
If variable can be `None`, don't be implicit. Be explicit.
7c2c190
to
2017bb8
Compare
Co-authored-by: Damien Elmes <[email protected]>
Co-authored-by: Damien Elmes <[email protected]>
Co-authored-by: Damien Elmes <[email protected]>
2017bb8
to
ff4bfcb
Compare
I'd appreciate it if you could make an attempt to review these yourself first - Anki wouldn't even start until the latest commit I pushed, and when I started reviewing the changes, I quickly encountered changes that don't appear to be correct, such as
|
Do you plan to return to this? I can keep it around if you're currently busy, as it would be a nice change. |
Fixed all errors within the directory
pylib
by simply adding assertions. These can be replaced later on by better solutions. But we can now set the optionstrict_optional
totrue
for the entire directorypylib
.Why this option should be
true
: https://mypy.readthedocs.io/en/stable/config_file.html#confval-strict_optional