Type stubs for the lxml package

Overview

lxml-stubs

Python Tests pypi

About

This repository contains external type annotations (see PEP 484) for the lxml package.

Installation

To use these stubs with mypy, you have to install the lxml-stubs package.

pip install lxml-stubs

Contributing

Contributions should follow the same style guidelines as typeshed.

History

These type annotations were initially included included in typeshed, but lxml's annotations were found to be frequently problematic and have therefore been deleted from typeshed.

The code was extracted by Jelle Zijlstra from the original typeshed codebase and moved to a separate repository using git filter-branch.

Authors

Numerous people have contributed to the lxml stubs; see the git history for details.

Comments
  • Removes a test dependency that can't be installed

    Removes a test dependency that can't be installed

    This is a first approach to make the test suite run again.

    I think it's fine not to rely on a pytest-plugin that requires a templating language and run mypy directly instead.

    However,

    • that mypy run yields errors
    • ~~at least locally the package can't be installed for Python 3.6~~ (not an issue w/ the CI platform)
    • ~~at least locally~~ pytest doesn't run tests; i leave that to someone acquainted with yaml-based test definitions
    opened by funkyfuture 6
  • No stubs for lxml.html

    No stubs for lxml.html

    Running mypy foo.py with lxml-stubs installed handles import lxml fine, but import lxml.html reports an error since it is not defined in lxml-stubs.

    foo.py:2: error: Skipping analyzing 'lxml.html': found module but no type hints or library stubs
    foo.py:2: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
    Found 1 error in 1 file (checked 1 source file)
    

    foo.py file:

    import lxml
    import lxml.html
    
    enhancement good first issue 
    opened by egrubbs 6
  • Create public aliases for arguments

    Create public aliases for arguments

    The problem I'm trying to solve is having a method that takes an element or elementtree as argument, and forwards that to a XPath object.

    x = etree.XPath("//div")
    
    def method(elem):
      // other stuff
      r = x(elem)
      // other stuff
    

    If I do this with fx pylance, I would get the complaint that elem is Unknown. If I try to fix this by adding the type, I would have to write

    def method(elem: typing.Union[etree._Element, etree._ElementTree]):
    

    Which of course triggers that the types are private.

    For easy fix I would suggest some simple typing aliases

    TElement = _Element or something like that (Don't really care about the syntax - more about that the types would be public)

    invalid 
    opened by KalleDK 6
  • stub names raise warning

    stub names raise warning

    If I have the following

    from lxml import etree
    tree: etree._ElementTree = etree.parse(file_obj)
    

    I get a pylint warning: W0212: Access to a protected member _ElementTree of a client class (protected-access)

    I see _Element and _ElementTree are the lxml names, I gather a workaround is outside the scope of this project?

    wontfix 
    opened by altaurog 5
  • Argument

    Argument "nsmap" to "Element" has incompatible type

    This simplified code reproduces an error I am seeing when running mypy over my project.

    from lxml import etree
    
    
    def fcn() -> None:
        namespaces = {"a": "http://www.w3.org/2001/XMLSchema"}
        etree.Element(
            "{{{scl_namespaces['xs']}}}pattern", attrib={"value": "0"}, nsmap=namespaces
        )
    
    
    if __name__ == "__main__":
        fcn()
    

    The following error is produced.

    c:\>mypy --strict -m lxml_mypy
    lxml_mypy.py:7: error: Argument "nsmap" to "Element" has incompatible type "Dict[str, str]"; expected "Union[Dict[Optional[bytes], bytes], Dict[Optional[str], str], None]"
    Found 1 error in 1 file (checked 1 source file)
    

    I am using:

    • python 3.9.5
    • lxml 4.6.3
    • lxml-stubs 0.2.0

    I did some searching and found TypeVar and TypedDict but neither of those seemed to be able to address the issue. Does anyone have suggestions on how to resolve this typing error?

    Please let me know if you need more info and thanks in advance.

    opened by keith-gray-powereng 5
  • Add `Pathlike` as valid type for file arguments

    Add `Pathlike` as valid type for file arguments

    See https://github.com/lxml/lxml/pull/337

    Also created alias _FileSource for valid types for file arguments. I'm not sure how this should be added since it was only added in the newest release of lxml.

    opened by janssenhenning 4
  • CustomElementClassLookup return type

    CustomElementClassLookup return type

    You approved my PR #8 a few days ago to add types for CustomElementClassLookup.

    The lookup method of that class, according to the docs should return a subclass of ElementBase or None. Because it is returning a class, not an instance of a class, the return type should be Optional[Type[ElementBase]].

    This was changed to Optional[ElementBase] in c6120e4997aa59535d434b03f1433fdbd1f2fdc9. I think this change should be reverted as it's incorrect according to the documentation and the required implementation of the function.

    Thanks for the help and for approving the original PR.

    opened by AidanWoolley 4
  • Suggestion: disable format check in PR and normal commits

    Suggestion: disable format check in PR and normal commits

    Although format checks can help users reading the code with more consistency, the way it's done currently is a burden for both contributor and maintainer alike.

    1. In particular, it insists on changing function / method layout every now and then, making patch harder to read.
    2. Besides, format error in github workflow worker can potentially hide typing error. If format error is found, mypy could stop checking as well, depending on the execution order.

    mypy check is much more important, preventing code error to manifest in annotation for too long. Given how lxml-stubs is maintained currently, it's good enough for format check to be performed manually only once, just before tagged release.

    opened by abelcheung 3
  • Add `etree.iselement`

    Add `etree.iselement`

    Using TypeGuard from python 3.10 via typing-extensions. However, I noticed that lxml-stubs has no dependency on typing-extensions even though it's already used. Should this be added?

    opened by janssenhenning 3
  • Unresolved attribute reference 'findtext' for class '_Element'

    Unresolved attribute reference 'findtext' for class '_Element'

    from lxml import etree
    
    xml_string = b"""<?xml version="1.0" encoding="UTF-8"?>
    <searchResults>
      <resultCount>59</resultCount>
      <Book>
        <bookId>30323794902</bookId>
      </Book>
    </searchResults>
    """
    root = etree.fromstring(xml_string)
    books = root.findall("Book")
    for book in books:
        print(book.findtext("bookId"))  # Unresolved attribute reference 'findtext' for class '_Element' 
    
    enhancement good first issue 
    opened by louwers 3
  • Publishing as package in the cheeseshop

    Publishing as package in the cheeseshop

    it seems that there's already a lxml-stubs package on the PyPI.

    i'm opening this issue because the situation will lead to confusion and the docs should warn about installing this package.

    or could the stubs be integrated w/ the lxml package?

    opened by funkyfuture 3
  • Add stub for lxml.html.HtmlElement and adjust function return types

    Add stub for lxml.html.HtmlElement and adjust function return types

    I'm not entirely sure whether always returning HtmlElement from the lxml.html.*fromstring functions is 100% correct – as far as I understand, the actual return type depends on the parser that is used, and therefore can actually be plain _Elements like it was before.

    Do you think it makes sense to @overload these functions depending on the passed parser?

    opened by Wuestengecko 0
  • Inconsistency regarding namespace mappings

    Inconsistency regarding namespace mappings

    by this comment i felt strongly encouraged refactor code in order to use an empty string as key for the default namespace in a namespace mapping, but soon found inconsistencies with the current annotations and implementation.

    first, mypy complains about this:

    an_element = etree.fromstring("<element/>")
    a_new_element = an_element.makeelement("new", nsmap=an_element.nsmap)
    

    with error: Argument "nsmap" to "makeelement" of "_Element" has incompatible type "Dict[Optional[str], str]"; expected "Optional[Mapping[str, str]]".

    then, the empty string as key isn't even the default:

    In [1]: from lxml import etree
    In [2]: t = etree.fromstring("<element xmlns='test'/>")
    In[3]: t.nsmap.get(None)
    Out[3]: 'test'
    In[4]: t.nsmap.get("")
    Out[[4]:
    

    i have no proposal how to solve these issues, but i consider the first demo a bug and the latter at least confusing.

    opened by funkyfuture 0
  • Annotating XPathObject is a lost cause

    Annotating XPathObject is a lost cause

    Have been feeding on my own dogfood for quite a while, the annotation of XPathObject as XPath evaluation result is one of the spots that I feel constantly irritated. Although the annotation itself is correct per se, its presence turns out to be a nuisance for developers. The problem is, it is a long union of so many types that the selection result can never be used directly. For xpath evluation result to be useful, it has to be narrowed down to specific type(s) with approaches like:

    • try-except block
    • isinstance() check
    • assert

    All of them have one thing in common: throw away the type supplied by stub, and perform manual type narrowing afterwards. It is more like a roadblock rather than something that helps. As a supporting example, elementpath package returns Any as evaluation result even when the package is considered fully annotated.

    Input argument used by variable inside xpath expression is different though, as that doesn't need extra processing and isn't as complex as output types.

    My suggestion is set XPathObject as alias to Any, and cleanup input arguments as another alias, like:

    _XPathObject = Any
    _XPathVarArg = Union[...]
    class XPath:
        def __call__(self,
            _etree_or_element: _ElementOrTree,
            **_variables: _XPathVarArg
        ) -> _XPathObject: ...
    
    opened by abelcheung 3
  • Validating documents with etree.XMLSchema

    Validating documents with etree.XMLSchema

    According to the documentation it is possible to validate an ElementTree object against an XML schema using etree.XMLSchema.validate() method or using it as a callable. In both cases, mypy traces an error. E.g.:

    import sys
    
    from lxml import etree
    
    
    def main() -> None:
        try:
            schema_file = sys.argv[1]
            file = sys.argv[2]
        except IndexError:
            sys.exit("Missing argument(s)")
    
        schema = etree.XMLSchema(etree.parse(schema_file))
        doc = etree.parse(file)
    
        if schema(doc):
            print(f"{file} is valid")
        else:
            print(f"{file} is not valid")
    
        if schema.validate(doc):
            print(f"{file} is valid")
        else:
            print(f"{file} is not valid")
    
    
    if __name__ == '__main__':
        main()
    
    % mypy main.py     
    main.py:16: error: "XMLSchema" not callable
    main.py:21: error: "XMLSchema" has no attribute "validate"
    
    opened by lmar76 0
Releases(0.4.0)
An open-source, mini imitation of GitHub Copilot for Emacs.

Second Mate An open-source, mini imitation of GitHub Copilot using EleutherAI GPT-Neo-2.7B (via Huggingface Model Hub) for Emacs. This is a much small

Sam Rawal 238 Dec 27, 2022
Flake8 wrapper to make it nice, legacy-friendly, configurable.

THE PROJECT IS ARCHIVED Forks: https://github.com/orsinium/forks It's a Flake8 wrapper to make it cool. Lint md, rst, ipynb, and more. Shareable and r

Life4 232 Dec 16, 2022
Stubs with type annotations for ordered-set Python library

ordered-set-stubs - stubs with type annotations for ordered-set Python library Archived - now type annotations are the part of the ordered-set library

Roman Inflianskas 2 Feb 06, 2020
Collection of awesome Python types, stubs, plugins, and tools to work with them.

Awesome Python Typing Collection of awesome Python types, stubs, plugins, and tools to work with them. Contents Static type checkers Dynamic type chec

TypedDjango 1.2k Jan 04, 2023
Type annotations builder for boto3 compatible with VSCode, PyCharm, Emacs, Sublime Text, pyright and mypy.

mypy_boto3_builder Type annotations builder for boto3-stubs project. Compatible with VSCode, PyCharm, Emacs, Sublime Text, mypy, pyright and other too

Vlad Emelianov 2 Dec 05, 2022
Pylint plugin to enforce some secure coding standards for Python.

Pylint Secure Coding Standard Plugin pylint plugin that enforces some secure coding standards. Installation pip install pylint-secure-coding-standard

Nguyen Damien 2 Jan 04, 2022
A static type analyzer for Python code

pytype - 🦆 ✔ Pytype checks and infers types for your Python code - without requiring type annotations. Pytype can: Lint plain Python code, flagging c

Google 4k Dec 31, 2022
flake8 plugin to catch useless `assert` statements

flake8-useless-assert flake8 plugin to catch useless assert statements Download or install on the PyPI page Violations Code Description Example ULA001

1 Feb 12, 2022
Flake8 plugin that checks import order against various Python Style Guides

flake8-import-order A flake8 and Pylama plugin that checks the ordering of your imports. It does not check anything else about the imports. Merely tha

Python Code Quality Authority 270 Nov 24, 2022
Tool for automatically reordering python imports. Similar to isort but uses static analysis more.

reorder_python_imports Tool for automatically reordering python imports. Similar to isort but uses static analysis more. Installation pip install reor

Anthony Sottile 589 Dec 26, 2022
Flake8 plugin to find commented out or dead code

flake8-eradicate flake8 plugin to find commented out (or so called "dead") code. This is quite important for the project in a long run. Based on eradi

wemake.services 277 Dec 27, 2022
An enhanced version of the Python typing library.

typingplus An enhanced version of the Python typing library that always uses the latest version of typing available, regardless of which version of Py

Contains 6 Mar 26, 2021
flake8 plugin which checks that typing imports are properly guarded

flake8-typing-imports flake8 plugin which checks that typing imports are properly guarded installation pip install flake8-typing-imports flake8 codes

Anthony Sottile 50 Nov 01, 2022
Pylint plugin for improving code analysis for when using Django

pylint-django About pylint-django is a Pylint plugin for improving code analysis when analysing code using Django. It is also used by the Prospector t

Python Code Quality Authority 544 Jan 06, 2023
A simple program which checks Python source files for errors

Pyflakes A simple program which checks Python source files for errors. Pyflakes analyzes programs and detects various errors. It works by parsing the

Python Code Quality Authority 1.2k Dec 30, 2022
A plugin for Flake8 that provides specializations for type hinting stub files

flake8-pyi A plugin for Flake8 that provides specializations for type hinting stub files, especially interesting for linting typeshed. Functionality A

Łukasz Langa 58 Jan 04, 2023
flake8 plugin that integrates isort

Flake8 meet isort Use isort to check if the imports on your python files are sorted the way you expect. Add an .isort.cfg to define how you want your

Gil Forcada Codinachs 139 Nov 08, 2022
Static type checker for Python

Static type checker for Python Speed Pyright is a fast type checker meant for large Python source bases. It can run in a “watch” mode and performs fas

Microsoft 9.2k Jan 03, 2023
Run isort, pyupgrade, mypy, pylint, flake8, and more on Jupyter Notebooks

Run isort, pyupgrade, mypy, pylint, flake8, mdformat, black, blacken-docs, and more on Jupyter Notebooks ✅ handles IPython magics robustly ✅ respects

663 Jan 08, 2023
Unbearably fast O(1) runtime type-checking in pure Python.

Look for the bare necessities, the simple bare necessities. Forget about your worries and your strife. — The Jungle Book.

beartype 1.4k Jan 01, 2023