-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix remaining typing issues for PyLinter
and related functions
#5663
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -537,7 +537,7 @@ def make_options() -> Tuple[Tuple[str, OptionDict], ...]: | |
def __init__( | ||
self, | ||
options=(), | ||
reporter=None, | ||
reporter: Union[reporters.BaseReporter, reporters.MultiReporter, None] = None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to do this in a separate PR but some of the issues fixed here didn't show if I didn't add typing to at least one of these parameters. |
||
option_groups=(), | ||
pylintrc=None, | ||
): | ||
|
@@ -1496,7 +1496,11 @@ def _add_one_message( | |
msg_cat = MSG_TYPES[message_definition.msgid[0]] | ||
self.msg_status |= MSG_TYPES_STATUS[message_definition.msgid[0]] | ||
self.stats.increase_single_message_count(msg_cat, 1) | ||
self.stats.increase_single_module_message_count(self.current_name, msg_cat, 1) | ||
self.stats.increase_single_module_message_count( | ||
self.current_name, # type: ignore[arg-type] # Should be removable after https://github.com/PyCQA/pylint/pull/5580 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
msg_cat, # type: ignore[arg-type] # Mypy doesn't see a Final dict as dict of literals | ||
DanielNoord marked this conversation as resolved.
Show resolved
Hide resolved
|
||
1, | ||
) | ||
try: | ||
self.stats.by_msg[message_definition.symbol] += 1 | ||
except KeyError: | ||
|
@@ -1613,6 +1617,9 @@ def _set_one_msg_status( | |
) -> None: | ||
"""Set the status of an individual message""" | ||
if scope == "module": | ||
if not isinstance(line, int): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. I think asserts are the canonical way to convey "I'm doing that to appease the mypy god". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, although both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some typing can be declared beforehand at no cost (I think we used that for the message id store). But it only work when it's actually impossible for line to not be an int. I think the only way is to change the design which might be costly (and/or overkill). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you know that it's never
|
||
raise ValueError | ||
|
||
self.file_state.set_msg_status(msg, line, enable) | ||
if not enable and msg.symbol != "locally-disabled": | ||
self.add_message( | ||
|
@@ -1641,7 +1648,9 @@ def _get_messages_to_set( | |
else: | ||
category_id_formatted = category_id | ||
if category_id_formatted is not None: | ||
for _msgid in self.msgs_store._msgs_by_category.get(category_id_formatted): | ||
for _msgid in self.msgs_store._msgs_by_category.get( | ||
category_id_formatted, [] | ||
): | ||
message_definitions.extend( | ||
self._get_messages_to_set(_msgid, enable, ignore_unknown) | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,7 +130,7 @@ def set_msg_status(self, msg: "MessageDefinition", line: int, status: bool) -> N | |
self._module_msgs_state[msg.msgid] = {line: status} | ||
|
||
def handle_ignored_message( | ||
self, state_scope: Optional[Literal[0, 1, 2]], msgid: str, line: int | ||
self, state_scope: Optional[Literal[0, 1, 2]], msgid: str, line: Optional[int] | ||
) -> None: | ||
"""Report an ignored message. | ||
|
||
|
@@ -139,6 +139,8 @@ def handle_ignored_message( | |
or globally. | ||
""" | ||
if state_scope == MSG_STATE_SCOPE_MODULE: | ||
if not isinstance(line, int): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar problem here: if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't it an issue to fix in astroid ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No this is a pylint issue. If we disable something via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All right, thank you for clearing that up. Maybe we could create a different structure to handle module and package disable, but I'm not very clear on the state of the code right now. I'll have a look before 2.13.0 is released. (Or I trust you if you find a solution that you think is good enough in the meantime). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. Either |
||
raise ValueError | ||
try: | ||
orig_line = self._suppression_mapping[(msgid, line)] | ||
self._ignored_msgs[(msgid, orig_line)].add(line) | ||
|
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.
Although
mypy
doesn't recognise that because thisdict
is final its values could be seen asLiteral
instead ofstr
it does "allow" us to add an ignore later on.We will be warned if
MSG_TYPES
is being altered and we can thus be sure that any of the values obtained from it is one of the values already in there.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 isn't really a concept of final dicts that I know of (at this time). However there might be two separate workaround that you could try.
Dict[Literal["I", "C", ...], Literal["info", "convention", ...]
That should work but doesn't guarantee a mapping between specific keys and values.TypedDict
and using that instead ofFinal
for the annotation. That only works for string keys, but in this case it could be exactly what you're looking for. (It might even be worth a suggestion on the mypy / pyright issue tracker?)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.
It could be that you then need a
cast
formessage_definition.msgid[0]
though.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.
@cdce8p Do you know if there is any plans or previous discussion about the concept of final dicts? I saw a similar issue about final frozen sets I think, which would also be beneficial to us.
I can change this to
TypedDict
, but if there are plans to (at some point) support final dicts I think it makes sense to keep it like this as it "better" represents what this actually is.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 don't know of any plans. If
TypedDicts
work, they might be the easiest solution here.