{"id":2334,"library":"types-beautifulsoup4","title":"Typing Stubs for BeautifulSoup4","description":"types-beautifulsoup4 provides PEP 561 compliant type stubs for the beautifulsoup4 library, enabling static type checkers like MyPy, Pyright, and PyCharm to analyze code using BeautifulSoup4 for type correctness. This package specifically targets beautifulsoup4 versions up to 4.12.*. It is part of the typeshed project, which automatically releases updates to stub packages on PyPI, often in sync with the upstream library's releases or typeshed's internal machinery.","status":"active","version":"4.12.0.20250516","language":"en","source_language":"en","source_url":"https://github.com/python/typeshed","tags":["typing","stubs","beautifulsoup4","html-parsing","web-scraping","typeshed"],"install":[{"cmd":"pip install types-beautifulsoup4","lang":"bash","label":"Install stub package"}],"dependencies":[{"reason":"Provides the actual runtime library for which these are type stubs.","package":"beautifulsoup4","optional":false}],"imports":[{"note":"You import the types implicitly by having `types-beautifulsoup4` installed. You should never import directly from the `types-beautifulsoup4` package itself.","symbol":"BeautifulSoup","correct":"from bs4 import BeautifulSoup"},{"note":"Type stubs are automatically applied by type checkers when importing symbols from the `bs4` package.","symbol":"Tag","correct":"from bs4.element import Tag"}],"quickstart":{"code":"from bs4 import BeautifulSoup, Tag\n\nhtml_doc: str = \"\"\"\n<html>\n<head><title>The Title</title></head>\n<body>\n<p class=\"story\">Hello World</p>\n</body>\n</html>\n\"\"\"\n\nsoup: BeautifulSoup = BeautifulSoup(html_doc, 'html.parser')\n\n# Accessing a tag with type hint\ntitle_tag: Tag | None = soup.find('title')\nif title_tag:\n    print(f\"Title: {title_tag.text}\")\n\n# Accessing a paragraph with type hint\nparagraph_tag: Tag | None = soup.find('p', class_='story')\nif paragraph_tag:\n    print(f\"Paragraph: {paragraph_tag.text}\")\n","lang":"python","description":"This quickstart demonstrates how to use `beautifulsoup4` with type hints. With `types-beautifulsoup4` installed, a type checker will correctly infer the types of `soup`, `title_tag`, and `paragraph_tag` objects, enabling better autocompletion and error detection. Note the use of `| None` for `find()` results, as elements might not be present."},"warnings":[{"fix":"pip uninstall types-beautifulsoup4","message":"Starting with `beautifulsoup4` version 4.13.0, the `beautifulsoup4` package itself includes type annotations. If you are using `beautifulsoup4` 4.13.0 or newer, you MUST uninstall `types-beautifulsoup4` to avoid potential conflicts or incorrect type checking behavior.","severity":"breaking","affected_versions":"beautifulsoup4 >= 4.13.0"},{"fix":"Ensure `beautifulsoup4` is installed alongside `types-beautifulsoup4` (e.g., `pip install beautifulsoup4 types-beautifulsoup4`).","message":"Installing `types-beautifulsoup4` alone is not sufficient to run your code; you still need to install the `beautifulsoup4` runtime library. The `types-*` packages provide only type definitions, not the actual implementation.","severity":"gotcha","affected_versions":"All"},{"fix":"Pin your `types-beautifulsoup4` version to match the major.minor version of `beautifulsoup4` (e.g., `types-beautifulsoup4==4.12.*` for `beautifulsoup4==4.12.*`). Also, be aware that some stubs are partial and may not cover all APIs.","message":"Type checkers may report `Any` types or incorrect type inference if there's a version mismatch between the installed `types-beautifulsoup4` package and your `beautifulsoup4` runtime library, or if the stub package is marked as 'partial'. This can lead to a loss of type checking precision.","severity":"gotcha","affected_versions":"All"},{"fix":"Consider pinning your stub package versions (e.g., `types-beautifulsoup4==4.12.0.20250516`) in your `requirements.txt` to control when updates are applied, or review typeshed's changelog regularly.","message":"Typeshed, the source for `types-beautifulsoup4`, sometimes introduces stricter type annotations or changes that, while technically correct, might cause your existing code to fail type checking. This can happen even with minor updates to the stub package.","severity":"gotcha","affected_versions":"All"},{"fix":"Use type guards (`if element is not None:`) or `Optional` / `Union[..., None]` in your type annotations to explicitly handle cases where an element might not be found.","message":"Methods like `find()` and `find_all()` often return `None` if no matching element is found. Type checkers will correctly flag potential `None` access errors. Be explicit about handling optional return types in your code.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}