{"id":2831,"library":"types-pyasn1","title":"Typing stubs for pyasn1","description":"This package provides typing stubs for the `pyasn1` library, enabling static type checkers like MyPy and PyRight to validate code that uses `pyasn1`. It is part of the typeshed project, a collection of high-quality type annotations, and aims to provide accurate annotations for `pyasn1==0.6.*`. Typeshed releases stub packages automatically, often up to once a day, reflecting changes and improvements to the types.","status":"active","version":"0.6.0.20260408","language":"en","source_language":"en","source_url":"https://github.com/python/typeshed","tags":["typing","stubs","type-hints","asn.1","typeshed"],"install":[{"cmd":"pip install types-pyasn1","lang":"bash","label":"Install types-pyasn1"}],"dependencies":[{"reason":"This package provides type hints for `pyasn1`. The `pyasn1` library itself must be installed separately for runtime execution.","package":"pyasn1","optional":false}],"imports":[{"note":"This is a stub-only package. You import symbols from the `pyasn1` library directly, and `types-pyasn1` provides the necessary type hints for static analysis.","symbol":"Integer","correct":"from pyasn1.type import univ"},{"note":"This is a stub-only package. You import symbols from the `pyasn1` library directly, and `types-pyasn1` provides the necessary type hints for static analysis.","symbol":"Sequence","correct":"from pyasn1.type import univ"},{"note":"This is a stub-only package. You import symbols from the `pyasn1` library directly, and `types-pyasn1` provides the necessary type hints for static analysis.","symbol":"NamedType","correct":"from pyasn1.type import namedtype"}],"quickstart":{"code":"from pyasn1.type import univ, namedtype\nfrom pyasn1.codec.der import encoder, decoder\n\n# Define an ASN.1 structure (Record) using pyasn1 types\nclass Record(univ.Sequence):\n    componentType = namedtype.NamedTypes(\n        namedtype.NamedType('id', univ.Integer()),\n        namedtype.OptionalNamedType('name', univ.OctetString())\n    )\n\n# Create an instance and set values\nrecord = Record()\nrecord['id'] = 123  # Type checkers will ensure 'id' is an Integer\nrecord['name'] = univ.OctetString('Example Name') # Type checkers will ensure 'name' is an OctetString\n\n# Encode to DER\nencoded_data = encoder.encode(record)\nprint(f\"Encoded: {encoded_data.hex()}\")\n\n# Decode from DER (asn1Spec provides the expected type for decoding)\ndecoded_record, rest = decoder.decode(encoded_data, asn1Spec=Record())\nprint(f\"Decoded ID: {decoded_record['id']}\")\nprint(f\"Decoded Name: {decoded_record['name']}\")\n\n# Example of type checking: if 'id' was assigned a string, a type checker would flag it.\n# For instance, 'record['id'] = \"not_an_int\"' would cause a type error.\n","lang":"python","description":"This quickstart demonstrates how to define an ASN.1 `SEQUENCE` type, create an instance, populate it with data, and then encode and decode it using `pyasn1`. The `types-pyasn1` package ensures that static type checkers can validate the usage of `pyasn1` types and methods, catching potential type mismatches or incorrect attribute access at design time."},"warnings":[{"fix":"Ensure that your installed `pyasn1` version is compatible with the `types-pyasn1` version. The `types-pyasn1` version usually aligns its major/minor components with the targeted `pyasn1` version.","message":"The `types-pyasn1` package provides type annotations for a specific version range of `pyasn1` (currently `0.6.*`). Using it with a significantly different version of the runtime `pyasn1` library may lead to inaccurate type checking results or false positives/negatives.","severity":"gotcha","affected_versions":"<0.6.0 || >0.6.99"},{"fix":"Install `pyasn1` alongside `types-pyasn1` using `pip install pyasn1 types-pyasn1`.","message":"This is a 'stub-only' package, meaning it contains only type hint files (`.pyi`) and no runtime Python code. You must install the actual `pyasn1` library separately for your code to execute. `types-pyasn1` is only for static analysis.","severity":"gotcha","affected_versions":"all"},{"fix":"For strict dependency management, consider pinning the full version string including the date, or understand that the daily component might update even if the `X.Y.Z` part remains the same.","message":"Typeshed stub package versions follow a `X.Y.Z.YYYYMMDD` format. The `X.Y.Z` part indicates the `pyasn1` version the stubs target, while `YYYYMMDD` is the stub release date. Pinning only `X.Y.Z` (e.g., `types-pyasn1==0.6.0`) might still result in updates from newer daily stub releases if not fully pinned (e.g., `types-pyasn1==0.6.0.20260408`).","severity":"gotcha","affected_versions":"all"},{"fix":"Regularly review type checker output after updating `types-pyasn1`. If new type errors appear, adjust your code to satisfy the more accurate type hints or consider temporarily pinning to an older, compatible stub version while addressing the issues.","message":"While typeshed aims for stability, changes within the stub files themselves can introduce stricter type checks or corrections that may cause existing, untyped, or loosely typed code using `pyasn1` to suddenly fail type checking, even if the runtime `pyasn1` version has not changed.","severity":"breaking","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}