Skip to content
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

Added findtext and xpath #47

Closed
wants to merge 9 commits into from
Closed

Conversation

dispread
Copy link

I dont know why they werent already there so I added them.

Peace out

def findall(
self, path: _ElemPathArg, namespaces: _NSMapArg | None = None
) -> list[HtmlElement]: ...
def iterfind(
self, path: _ElemPathArg, namespaces: _NSMapArg | None = None
) -> Iterator[HtmlElement]: ...
def xpath(
Copy link
Contributor

Choose a reason for hiding this comment

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

🚫 [mypy] reported by reviewdog 🐶
Signature of "xpath" incompatible with supertype "_Element" [override]

self,
_path: _AnyStr,
*,
namespaces: _NonDefaultNSMapArg | None = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

🚫 [mypy] reported by reviewdog 🐶
Name "_NonDefaultNSMapArg" is not defined [name-defined]

_path: _AnyStr,
*,
namespaces: _NonDefaultNSMapArg | None = None,
extensions: _XPathExtFuncArg | None = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

🚫 [mypy] reported by reviewdog 🐶
Name "_XPathExtFuncArg" is not defined [name-defined]

namespaces: _NonDefaultNSMapArg | None = None,
extensions: _XPathExtFuncArg | None = None,
smart_strings: bool = True,
**_variables: _XPathVarArg
Copy link
Contributor

Choose a reason for hiding this comment

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

🚫 [mypy] reported by reviewdog 🐶
Name "_XPathVarArg" is not defined [name-defined]

extensions: _XPathExtFuncArg | None = None,
smart_strings: bool = True,
**_variables: _XPathVarArg
) -> list[HTMLElement]: ...
Copy link
Contributor

Choose a reason for hiding this comment

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

🚫 [mypy] reported by reviewdog 🐶
Name "HTMLElement" is not defined [name-defined]

@@ -195,12 +195,27 @@ class HtmlElement(etree.ElementBase):
def find(
self, path: _ElemPathArg, namespaces: _NSMapArg | None = None
) -> HtmlElement | None: ...
def findtext(
Copy link
Contributor

Choose a reason for hiding this comment

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

🚫 [pyright] reported by reviewdog 🐶
Method "findtext" overrides class "_Element" in an incompatible manner
  Parameter "namespaces" is missing in override
  Return type mismatch: base method returns type "str | None", override returns type "str | _T@findtext" (reportIncompatibleMethodOverride)

self,
_path: _AnyStr,
*,
namespaces: _NonDefaultNSMapArg | None = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

🚫 [pyright] reported by reviewdog 🐶
Type of parameter "namespaces" is partially unknown
  Parameter type is "Unknown | None" (reportUnknownParameterType)

self,
_path: _AnyStr,
*,
namespaces: _NonDefaultNSMapArg | None = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

🚫 [pyright] reported by reviewdog 🐶
"_NonDefaultNSMapArg" is not defined (reportUndefinedVariable)

_path: _AnyStr,
*,
namespaces: _NonDefaultNSMapArg | None = None,
extensions: _XPathExtFuncArg | None = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

🚫 [pyright] reported by reviewdog 🐶
Type of parameter "extensions" is partially unknown
  Parameter type is "Unknown | None" (reportUnknownParameterType)

_path: _AnyStr,
*,
namespaces: _NonDefaultNSMapArg | None = None,
extensions: _XPathExtFuncArg | None = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

🚫 [pyright] reported by reviewdog 🐶
"_XPathExtFuncArg" is not defined (reportUndefinedVariable)

namespaces: _NonDefaultNSMapArg | None = None,
extensions: _XPathExtFuncArg | None = None,
smart_strings: bool = True,
**_variables: _XPathVarArg
Copy link
Contributor

Choose a reason for hiding this comment

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

🚫 [pyright] reported by reviewdog 🐶
Type of parameter "_variables" is unknown (reportUnknownParameterType)

namespaces: _NonDefaultNSMapArg | None = None,
extensions: _XPathExtFuncArg | None = None,
smart_strings: bool = True,
**_variables: _XPathVarArg
Copy link
Contributor

Choose a reason for hiding this comment

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

🚫 [pyright] reported by reviewdog 🐶
"_XPathVarArg" is not defined (reportUndefinedVariable)

extensions: _XPathExtFuncArg | None = None,
smart_strings: bool = True,
**_variables: _XPathVarArg
) -> list[HTMLElement]: ...
Copy link
Contributor

Choose a reason for hiding this comment

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

🚫 [pyright] reported by reviewdog 🐶
"HTMLElement" is not defined (reportUndefinedVariable)

@dispread
Copy link
Author

Guess i found out

@dispread dispread marked this pull request as draft April 12, 2024 12:38
def cssselect(
self,
expr: str,
*,
translator: _CSSTransArg = "xml",
) -> list[HtmlElement]: ...
) -> _XPathObject: ...
Copy link
Contributor

Choose a reason for hiding this comment

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

🚫 [mypy] reported by reviewdog 🐶
Name "_XPathObject" is not defined [name-defined]

def cssselect(
self,
expr: str,
*,
translator: _CSSTransArg = "xml",
) -> list[HtmlElement]: ...
) -> _XPathObject: ...
Copy link
Contributor

Choose a reason for hiding this comment

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

🚫 [pyright] reported by reviewdog 🐶
"_XPathObject" is not defined (reportUndefinedVariable)

Hopefully works now
@@ -195,18 +198,33 @@ class HtmlElement(etree.ElementBase):
def find(
self, path: _ElemPathArg, namespaces: _NSMapArg | None = None
) -> HtmlElement | None: ...
def findtext(
Copy link
Contributor

Choose a reason for hiding this comment

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

🚫 [mypy] reported by reviewdog 🐶
Signature of "findtext" incompatible with supertype "_Element" [override]

@@ -195,18 +198,33 @@ class HtmlElement(etree.ElementBase):
def find(
self, path: _ElemPathArg, namespaces: _NSMapArg | None = None
) -> HtmlElement | None: ...
def findtext(
Copy link
Contributor

Choose a reason for hiding this comment

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

🚫 [pyright] reported by reviewdog 🐶
Method "findtext" overrides class "_Element" in an incompatible manner
  Positional parameter count mismatch; base method has 3, but override has 4
  Parameter "namespaces" is missing in override
  Return type mismatch: base method returns type "str | None", override returns type "str | _T@findtext" (reportIncompatibleMethodOverride)

@dispread dispread marked this pull request as ready for review April 12, 2024 13:11
@dispread
Copy link
Author

I swear I'm not retarded, just tired

@abelcheung
Copy link
Owner

abelcheung commented Apr 12, 2024

@dispread No sweat, please do it at your own pace. I'll check again tomorrow, in case you have additional commits related to this change.

BTW I want to caution on something:

dont know why they werent already there

When a method is absent in a subclass, this means the corresponding method in superclass would be used (etree._Element in this case).

@abelcheung
Copy link
Owner

@dispread If these changes are all you intended to submit, then they are not needed. As I said before, if the methods don't exist in HtmlElement, the corresponding definition of findtext() and xpath() in its superclasses (etree._Element in this case) would be picked up.

Feel free to reopen this PR if you have other changes in mind.

@abelcheung abelcheung closed this Apr 14, 2024
@dispread
Copy link
Author

@abelcheung
Just out of curiosity, what IDE are you testing this on? I am using Pycharm and it doesn't seem to recognize the inherited methods hence my commits. Even on my branch it defaults to showing the autogenerated stubs, which is better than original (no hint on inherited Html element methods).
Is there something I'm missing?

Thanks in advance 👍

@abelcheung
Copy link
Owner

I see. I'm using vscode with pylance extension, and everything just works automatically. If pycharm really needs a manual override of methods in order to show up, that could indicate either oversight of setting or a missing feature. Even in terms of pure stub itself (disregarding any IDE support), this manual override shouldn't be needed at all. Guess I need to spend some time to get #8 going then.

@dispread
Copy link
Author

If I'll have some free time tomorrow/wednesday I can look into whether it's just my config or something reproducible.

@abelcheung
Copy link
Owner

abelcheung commented Apr 16, 2024

I can reproduce the problem you have faced with a default installation of Pycharm 2024.1. The autocompletion popup manages to show ElementBase.__init__() (The direct superclass of HtmlElement), but doesn't go beyond that. My current bet is that, _Element is deemed a private class, so none of its methods would show up in public class like HtmlElement. Same applies for ElementBase._init().

I manage to find a temporary hack but it's quite ugly. Using the example below:

from lxml import html
elem = html.Element('div')
result = elem.  # <- no xpath() in autocomplete

At this point, try to enforce the type of elem to _Element temporarily, like:

from lxml.etree import _Element
elem: _Element = html.Element('div')

Then the signature of xpath() would show up. After using, delete the temp enforced type.

I Will try to continue investigating later on, for now perhaps you can also try out if vscode works fine for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants