{"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":"python","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":[{"wrong":"from lxml-stubs import etree","symbol":"etree","correct":"from lxml import etree"}],"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,"search_vec":"'2026.2.16':8 '3.9':73 'activ':35 'aim':60 'annot':13 'check':22 'complet':50 'comprehens':10 'cover':62 'enabl':19 'extern':11 'fork':51 'frequent':38 'html':84 'ide':31 'improv':30 'librari':18 'like':25,66 'lxml':3,6,17,56,64,79 'lxml-stub':55 'lxml.etree':67 'lxml.html':68 'lxml.objectify':70 'maintain':36 'major':63 'month':45 'multipl':42 'mypi':26 'newer':75 'often':40 'origin':54 'per':44 'project':58 'provid':9 'pyright':28 'python':72 'releas':41 'static':20,81 'static-typ':80 'stub':14,57,78 'submodul':65 'support':32 'tool':24 'type':2,5,12,21,77,82 'type-stub':76 'types-lxml':1,4 'updat':39 'version':7,43 'xml':83","created_at":"2026-04-11T01:44:32.239964+00:00","updated_at":"2026-04-16T23:39:32.179425+00:00","problems":{"verify_error":"Traceback (most recent call last):\n  File \"<string>\", line 1, in <module>\nModuleNotFoundError: No module named 'lxml'"},"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"2026.2.16","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/abelcheung/types-lxml","docs":null,"changelog":null,"pypi":"https://pypi.org/project/types-lxml/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["type-stubs"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-07-03","last_verified":"2026-08-28","next_check":"2026-07-10","install_tag":null}}