-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Type-hints: update to pass mypy checks with types-docutils 0.21.0.20240704 #12511
Type-hints: update to pass mypy checks with types-docutils 0.21.0.20240704 #12511
Conversation
…m 2024-07-04. * Reporter.get_source_and_line is added at runtime by the ``RSTState.runtime_init`` method. * Pass the RST parser state's ``Inliner`` when calling class roles (could this reflect an interface change, or is it a bug?). * Add a RST-parser-context generic type argument to ``RSTState`` type-hints.
A TypeVar is _not_ added here; the RSTState's generic type argument would only appear in a function return signature, and I'm not sure I can figure out what it _should_ be based on the way an RSTState object is instantiated. Relates-to commit 1b04ddb.
Ah: I had made an incorrect guess that it was the behaviour-change noted in the pull request description that caused the failures that appear in the
...so that's clearly related somehow to the introduction of one of the |
This seems to be #10974 (or, as it mentions - in fact the same problem reported in #10785). |
…b04ddb with ``Any`` generic type arg.
sphinx/util/nodes.py
Outdated
_RSTContext = TypeVar("_RSTContext") | ||
|
||
|
||
def nested_parse_with_titles(state: RSTState[_RSTContext], content: StringList, node: Node, |
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.
Why didn't the same WARNING: py:class reference target not found: _RSTContext [ref.class]
failure occur here?
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 -- perhaps it's because of the :param state:
line in the docstring of the former case; this function doesn't list parameters within its docstring?
However, it also seems that this function isn't listed in the generated documentation. That could be another reason that the warning doesn't appear.
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.
You don't need to have a type variable here. Just use a simple Any
. Why? because you don't use that specific type variable. A type variable without any bound or constraint is equivalent to Any.
There are some unsolved mysteries in here if anyone has time to help investigate; as-is, this would allow unblocking the |
sphinx/util/nodes.py
Outdated
_RSTContext = TypeVar("_RSTContext") | ||
|
||
|
||
def nested_parse_with_titles(state: RSTState[_RSTContext], content: StringList, node: Node, |
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.
You don't need to have a type variable here. Just use a simple Any
. Why? because you don't use that specific type variable. A type variable without any bound or constraint is equivalent to Any.
I agree the genericness is annoying to deal with -- I understand that @danieleades added it to account for the A |
I definitely wouldn't rule out that fixing this properly requires some upstream fixes in typeshed |
Actually: see A |
Thank you @AA-Turner @danieleades. cc @adamtheturtle for your consideration (ref: python/typeshed#12226 ) |
Should the context be generic in RSTState at all? Or should it be concrete in this subclass? |
No, it should be |
hang on, does that mean it should be |
Thanks all for the help; please consider all the mysteries here resolved! |
Feature or Bugfix
Purpose
types-docutils
has resulted in somemypy
linting failures; this changeset attempts to address those.Detail
This was less trivial than I expected; here are some of the resolved and unresolved questions I've encountered along the way:
Reporter.get_source_and_line
, themypy
type warning is ignored; the relevant attribute is added at runtime bydocutils
.RSTState
typehints. These could be declared in a single location and then re-used usingimport
. Is that worthwhile in this case? I'm not sure.types-docutils
, andRSTState
moved intosphinx.utils.typing
for convenience without any generic-binding divergence risk, so this is resolved.self.state.inliner
is passed to a callee that expects anInliner
type argument instead of the previousself.state
value. I'm not 100% certain whether this is correct; some more code archaeology (perhaps indocutils
too, in case the interface has changed) may be necessary.make_directive_and_state
function with the signature:def make_directive_and_state(*, env: SimpleNamespace, input_lines: StringList | None = None) -> tuple[RSTState, SphinxDirective]:
. Deciding on a generic type argument for theRSTState
entry in the return-tuple-type spec seems tricky. As an interim measure I've placedAny
in there.RSTState
in the upstream typestub library.Relates