Skip to content

add iterator methods to lxml #1647

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

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion third_party/3/lxml/etree.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,67 @@ _DictAnyStr = Union[Dict[str, str], Dict[bytes, bytes]]
_Dict_Tuple2AnyStr_Any = Union[Dict[Tuple[str, str], Any], Tuple[bytes, bytes], Any]


class ElementChildIterator(Iterator['_Element']):
class _ElementMatchIterator(Iterator['_Element']):
def __iter__(self) -> '_ElementMatchIterator': ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these overridden __iter__/__next__ methods seem to be redundant. I think you can just write most of these classes like class _ElementMatchIterator(Iterator[_Element]): .... __iter__ and __next__ will be inherited correctly. In that case, leave no blank space between the classes.

def __next__(self) -> '_Element': ...

class ElementChildIterator(_ElementMatchIterator):
def __iter__(self) -> 'ElementChildIterator': ...

class AncestorsIterator(_ElementMatchIterator):
def __iter__(self) -> 'AncestorsIterator': ...

class SiblingsIterator(_ElementMatchIterator):
def __iter__(self) -> 'SiblingsIterator': ...

class ElementDepthFirstIterator(Iterator['_Element']):
def __iter__(self) -> 'ElementDepthFirstIterator': ...
def __next__(self) -> '_Element': ...

class ElementTextIterator(Iterator[str]):
def __iter__(self) -> 'ElementTextIterator': ...
def __next__(self) -> str: ...

class _Element(Iterable['_Element']):
# Docs: http://lxml.de/api/lxml.etree._Element-class.html
def addprevious(self, element: '_Element') -> None: ...
def addnext(self, element: '_Element') -> None: ...
def clear(self) -> None: ...
# Attributes
def get(self, key: _AnyStr, default: Optional[_AnyStr] = ...) -> _AnyStr: ...
def set(self, key: str, value: str) -> None: ...
def xpath(self, _path: _AnyStr, namespaces: Optional[_DictAnyStr] = ..., extensions: Any = ..., smart_strings: bool = ..., **_variables: Any) -> Any: ...
# indeed returns a Union[bool, float, _AnyStr, List[Union[ElementBase, _AnyStr, Tuple[]]]]: ...
# http://lxml.de/xpathxslt.html#xpath-return-values
attrib = ... # type: MutableMapping[str, str]
text = ... # type: _AnyStr
tag = ... # type: str
def append(self, element: '_Element') -> '_Element': ...

# Iterators
def __iter__(self) -> ElementChildIterator: ...
def iter(self,
tag: Optional[Union[str, _Element]]=...,
*tags: Any) -> ElementDepthFirstIterator: ...
def iterancestors(self,
tag: Optional[Union[str, _Element]]=...,
*tags: Any) -> AncestorsIterator: ...
def iterchildren(self,
tag: Optional[Union[str, _Element]]=...,
reversed=False,
*tags: Any) -> ElementChildIterator: ...
def iterdescendants(self,
tag: Optional[Union[str, _Element]]=...,
*tags: Any) -> ElementDepthFirstIterator: ...
def itersiblings(self,
tag: Optional[Union[str, _Element]]=...,
preceding=False,
*tags: Any) -> SiblingsIterator: ...
def itertext(self,
tag: Optional[Union[str, _Element]]=...,
with_tail=True,
*tags: Any) -> ElementTextIterator: ...


class ElementBase(_Element): ...

Expand Down