{"id":2829,"library":"types-lxml","title":"types-lxml","description":"types-lxml, version 2026.2.16, provides comprehensive external type annotations (stubs) for the `lxml` library, enabling static type checking with tools like MyPy and Pyright, and improving IDE support. It is actively maintained with frequent updates, often releasing multiple versions per month, and is a more complete fork of the original `lxml-stubs` project. It aims to cover major lxml submodules like `lxml.etree`, `lxml.html`, and `lxml.objectify` for Python 3.9 and newer.","status":"active","version":"2026.2.16","language":"en","source_language":"en","source_url":"https://github.com/abelcheung/types-lxml","tags":["type-stubs","lxml","static-typing","xml","html"],"install":[{"cmd":"pip install types-lxml","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Provides type stubs for this library; necessary for runtime execution of code that types-lxml annotates.","package":"lxml"},{"reason":"Added as a dependency to utilize its inline annotation within types-lxml itself.","package":"BeautifulSoup4","optional":false}],"imports":[{"note":"types-lxml provides stubs for lxml; you import from lxml itself, and type checkers use the stubs.","symbol":"etree","correct":"from lxml import etree"},{"note":"types-lxml provides stubs for lxml; you import from lxml itself, and type checkers use the stubs.","symbol":"html","correct":"from lxml import html"}],"quickstart":{"code":"from lxml import etree\n\n# Example XML string\nxml_string: str = '<root><item id=\"1\">Apple</item><item id=\"2\">Banana</item></root>'\n\n# Parse the XML string\nroot: etree._Element = etree.fromstring(xml_string)\n\n# Find an element by tag name\nitem: etree._Element | None = root.find('item')\nif item is not None:\n    print(f\"First item: {item.text}\")\n\n# Find all elements with a specific tag\nall_items: list[etree._Element] = root.findall('item')\nfor i, element in enumerate(all_items):\n    item_id: str | None = element.get('id')\n    print(f\"Item {i+1} (ID: {item_id}): {element.text}\")\n\n# Create a new element and append it\nnew_element: etree._Element = etree.SubElement(root, 'item', id='3')\nnew_element.text = 'Cherry'\n\n# Print the modified XML\nprint(\"\\nModified XML:\")\nprint(etree.tostring(root, pretty_print=True).decode())","lang":"python","description":"This quickstart demonstrates basic XML parsing, element finding, and modification using `lxml.etree`. `types-lxml` enhances this experience by providing accurate type hints for `lxml` objects like `_Element` and `_ElementTree`, which are consumed by static type checkers and IDEs."},"warnings":[{"fix":"Upgrade your Python environment to 3.9 or later.","message":"Python 3.8 support was dropped since the 2025.11.25 release. Ensure your project uses Python 3.9 or newer.","severity":"breaking","affected_versions":">=2025.11.25"},{"fix":"Consider using Pyright for type checking projects that heavily rely on lxml. If using MyPy, be aware of potential strictness around lxml's dynamic behaviors.","message":"Pyright is generally the preferred type checker for lxml code over MyPy. lxml's attribute normalization can be too restrictive for MyPy, leading to potential false positives or difficulties.","severity":"gotcha","affected_versions":"all"},{"fix":"Ensure your `lxml` dependency is updated to a supported version (e.g., 5.4 or 6.0+).","message":"Since the 2025.08.25 release, `types-lxml` officially supports `lxml` versions 6.0 and 5.4, and no longer tests against `lxml` 4.9.","severity":"breaking","affected_versions":">=2025.08.25"},{"fix":"Example: `from __future__ import annotations` then `from lxml.etree import _Element` or `def my_func(element: \"_Element\"): ...`.","message":"When using specific lxml types, particularly internal ones like `_Element` or `_ElementTree` in type annotations in your own code, it's best practice to import them from `lxml` directly, not `types_lxml`. Also, use `from __future__ import annotations` or quote the type hints to avoid forward reference issues.","severity":"gotcha","affected_versions":"all"},{"fix":"Most users should stick to the default `types-lxml`. Only install `types-lxml-multi-subclass` if you have specific advanced subclassing requirements for lxml elements.","message":"Since the 2024.08.07 release, there are two versions of `types-lxml`: the default and `types-lxml-multi-subclass`. The latter is intended for specific needs involving the creation of multiple lxml element subclasses.","severity":"gotcha","affected_versions":">=2024.08.07"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}