-
Notifications
You must be signed in to change notification settings - Fork 5
Make List[Node] covariants #6
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
Conversation
docutils-stubs/nodes.pyi
Outdated
@@ -11,6 +11,7 @@ from typing import Any, Callable, Dict, Iterable, Iterator, List, Optional, over | |||
|
|||
__docformat__: str | |||
|
|||
T_co = TypeVar('T', bound=nodes.Node, covariant=True) |
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.
- By convention, the name of the first argument of TypeVar should be a name of variable on the left-hand side.
- TypeVar with name
T_co
looks like covariant type variable having upper boundobject
. A name indicating that its upper bound isnodes.Node
is more preferable.
Examples can be taken from the implementation of typing
module::
CT_co = TypeVar('CT_co', covariant=True, bound=type)
https://github.com/python/cpython/blob/f2a9d5c8378cd7eca90b3b197e2cc0989da55014/Lib/typing.py#L1181
docutils-stubs/nodes.pyi
Outdated
@@ -38,7 +39,7 @@ def ensure_str(s: str) -> str: ... | |||
|
|||
class Text(Node, str): | |||
tagname: str = ... | |||
children: Tuple[nodes.Node, ...] = ... | |||
children: Tuple[T_co, ...] = ... |
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.
By default, Tuple
is covariant because it is immutable, while List
is mutable.
So, this change is not necessary.
For more detail, refer following comment and PEP,
- Covariance or contravariance python/typing#2 (comment)
- https://www.python.org/dev/peps/pep-0484/#covariance-and-contravariance
"The read-only collection classes in typing are all declared covariant in their type variable (e.g. Mapping and Sequence)."
docutils-stubs/nodes.pyi
Outdated
@@ -93,7 +94,7 @@ class Element(Node): | |||
def __contains__(self, attr: str) -> bool: ... | |||
def get_language_code(self, fallback: str = ...) -> str: ... | |||
def append(self, item: nodes.Node) -> None: ... | |||
def extend(self, item: Iterable[nodes.Node]) -> None: ... | |||
def extend(self, item: Iterable[T_co]) -> None: ... |
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 change is not necessary for the same reason as Tuple
.
Minimum examples are following, from typing import *
bool_tuple: Tuple[bool, bool, bool] = (True, False, True)
int_tuple: Tuple[int, int, int] = bool_tuple # OK
bool_iterable: Iterable[bool] = (b for b in [True, False, True])
int_iterable: Iterable[int] = bool_iterable # OK
bool_list: List[bool] = [True, False, True]
int_list: List[int] = bool_list # type error |
f93053a
to
01a4528
Compare
Updated! |
1966eef
to
60eada6
Compare
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.
Unused TypeVar in manpage.pyi.
docutils-stubs/writers/manpage.pyi
Outdated
from typing import Any, Callable, Dict, List, Optional, Pattern, Tuple, Type | ||
from typing import Any, Callable, Dict, List, Optional, Pattern, Tuple, Type, TypeVar | ||
|
||
N_co = TypeVar('N_co', bound=nodes.Node, covariant=True) |
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.
TypeVar N_co
is not used in manpage.pyi file.
Would you aim at using this TypeVar in order to fix the type of Translator.colspec
?
colspecs: List[nodes.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.
oh, sorry. It was mistake. removed.
60eada6
to
81a22cd
Compare
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 think this changes are worthy of bumping up the version.
How about bumping version to 0.0.13
?
I'll ship this after merging this. But it should be separated this PR itself. |
No description provided.