8000 Make List[Node] covariants by tk0miya · Pull Request #6 · tk0miya/docutils-stubs · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 2 commits into from
Nov 28, 2018
Merged

Make List[Node] covariants #6

merged 2 commits into from
Nov 28, 2018

Conversation

tk0miya
Copy link
Owner
@tk0miya tk0miya commented Nov 26, 2018

No description provided.

@tk0miya tk0miya requested a review from cocoatomo November 26, 2018 17:37
8000
@@ -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)
Copy link
Collaborator

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 bound object. A name indicating that its upper bound is nodes.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

@@ -38,7 +39,7 @@ def ensure_str(s: str) -> str: ...

class Text(Node, str):
tagname: str = ...
children: Tuple[nodes.Node, ...] = ...
children: Tuple[T_co, ...] = ...
Copy link
Collaborator

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,

@@ -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: ...
Copy link
Collaborator

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.

@cocoatomo
Copy link
Collaborator

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

@tk0miya tk0miya force-pushed the covariant_Nodes branch 2 times, most recently from f93053a to 01a4528 Compare November 27, 2018 11:54
@tk0miya
Copy link
Owner Author
tk0miya commented Nov 27, 2018

Updated!

@tk0miya tk0miya force-pushed the covariant_Nodes branch 4 times, most recently from 1966eef to 60eada6 Compare November 28, 2018 02:28
Copy link
Collaborator
@cocoatomo cocoatomo left a 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.

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)
Copy link
Collaborator

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] = ...

Copy link
Owner Author

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.

Copy link
Collaborator
@cocoatomo cocoatomo left a 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?

@tk0miya
Copy link
Owner Author
tk0miya commented Nov 28, 2018

I'll ship this after merging this. But it should be separated this PR itself.

@cocoatomo cocoatomo merged commit ba78e78 into master Nov 28, 2018
@tk0miya tk0miya deleted the covariant_Nodes branch November 28, 2018 05:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
4349 None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0