-
-
Notifications
You must be signed in to change notification settings - Fork 586
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 annotations for _Error
#1022
Conversation
Oops, I saw some |
Codecov ReportBase: 98.21% // Head: 98.19% // Decreases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## main #1022 +/- ##
==========================================
- Coverage 98.21% 98.19% -0.03%
==========================================
Files 20 20
Lines 3529 3536 +7
Branches 537 537
==========================================
+ Hits 3466 3472 +6
- Misses 47 48 +1
Partials 16 16
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
Thanks for the PR! This is more or less what I was imagining you were talking about, though I don't see from reading it why it couldn't be done to |
I took a quick look just now at this again -- though only a minute or two -- but in that minute, yeah, I can't see why the intermediate class is helping. The relevant part of this PR seems like just adding the two diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py
index 55c3ae737..2bc85df43 100644
--- a/jsonschema/exceptions.py
+++ b/jsonschema/exceptions.py
@@ -6,6 +6,7 @@ from __future__ import annotations
from collections import defaultdict, deque
from pprint import pformat
from textwrap import dedent, indent
+from typing import ClassVar
import heapq
import itertools
@@ -20,9 +21,13 @@ _unset = _utils.Unset()
class _Error(Exception):
+
+ _word_for_schema_in_error_message: ClassVar[str]
+ _word_for_instance_in_error_message: ClassVar[str]
+
def __init__(
self,
- message,
+ message: str,
validator=_unset,
path=(),
cause=None, What am I missing on the point here? |
I did that in the other PR, but technically the code is still "incorrectly" typed. Using |
I'm still missing what you mean.
Also how can I reproduce whatever error you're trying to fix by adding this? |
My bad, I hadn't seen these were the only subclasses. Then again, the issue kind of remains: a = _Error()
a.validator = 1
a.validator_value = 1
a.instance = 1
a.schema = 1
str(a) This will try to access the However, I think that our time is best spent on fixing actual bugs rather than on what-ifs. I'll amend the PR to use the |
Thanks, merging. |
To show what I mean with putting a class "in between" the main
_Error
class and the classes that actually have the attribute that is being used in__str__
. This satisfies type checkers and has minimal runtime impact, while making the code a bit cleaner.Let me know if this is something you would consider!
📚 Documentation preview 📚: https://python-jsonschema--1022.org.readthedocs.build/en/1022/