Skip to main content

Parse Python classes and attributes from a string of Python code.

Project description

PyClassParser

PyClassParser

CI PyPI version Python versions License: MIT DeepWiki

PyClassParser is a Python library designed to parse Python class definitions and extract class attributes. Parsing is backed by the standard-library ast module, so it is robust against blank lines, comments, docstrings, methods, nested classes and default values that contain colons. It uses the attrs library for defining the result objects and ensures type safety with attrs-strict.

Features

  • Parses Python class definitions to extract class names and their attributes.
  • Uses the standard-library ast module, so the input must be syntactically valid Python (a SyntaxError is raised otherwise).
  • Uses attrs for the result objects and attrs-strict for type validation.
  • Captures each attribute's name (attr_value), annotation (attr_type) and default value (default_value) when present.
  • Rebuilds a clean, always-valid reconstruction of the imports and class stubs.

Installation with UV:

uv add pyclassparser

Usage

Here's how you can use PyClassParser to parse Python class definitions:

Example Code

Create a Python script, for example example.py:

from pyclassparser import ClassParser

code = """
class MyClass:
    attr1: int
    attr2: str = "default"

class AnotherClass:
    attr3: float
"""
parser = ClassParser(code=code)
print(parser.output)
print(parser.class_data_dict)

Example Output

Running the above script should give you the parsed output of class definitions:

class MyClass:
    attr1: int
    attr2: str = 'default'

class AnotherClass:
    attr3: float

{'MyClass': [ClassATTR(attr_value='attr1', attr_type='int', default_value=None),
             ClassATTR(attr_value='attr2', attr_type='str', default_value="'default'")],
 'AnotherClass': [ClassATTR(attr_value='attr3', attr_type='float', default_value=None)]}

Note: parser.parsed_code is an alias of parser.output. Because the code is re-emitted from the AST, string literals are normalized (e.g. "default" becomes 'default').

What gets parsed

  • Only top-level classes are reported; nested classes are ignored.
  • An attribute is either an annotated assignment (attr: type), where attr_type is the annotation, or a plain assignment (attr = value), where attr_type is None. default_value holds the default as it appears in the source, or None when there is no default.
  • Docstrings, comments, methods and nested classes are ignored.
  • Class-level decorators and PEP 695 type parameters (class Box[T]:) are kept in parsed_code.
  • A duplicate top-level class name raises ValueError.

Running the tests

uv sync
uv run pytest
uv run mypy pyclassparser

Contributing

Contributions are welcome! Please submit a pull request or open an issue to discuss your ideas.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyclassparser-0.8.1.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyclassparser-0.8.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file pyclassparser-0.8.1.tar.gz.

File metadata

  • Download URL: pyclassparser-0.8.1.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for pyclassparser-0.8.1.tar.gz
Algorithm Hash digest
SHA256 e96b94dfcbcdf86df0cc9a40dbc53b4b76889ca8a0ff9ae703c9353dde02e6b8
MD5 65b921434d3a3a8a46bf10ba31b7f76a
BLAKE2b-256 5c3e5e908aed4b6fc90f0a73a86384b98fd4ca05bc4dca72a7c9633c4fdb2155

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyclassparser-0.8.1.tar.gz:

Publisher: release.yml on carlosplanchon/pyclassparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyclassparser-0.8.1-py3-none-any.whl.

File metadata

  • Download URL: pyclassparser-0.8.1-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for pyclassparser-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ed1a02956c49543ae89d86a1a041c67cd79bcbf6e0d7597db3fa86fbd677a102
MD5 ffd3d54fa296b0f878be033e7f14cbcd
BLAKE2b-256 98bc99a6037f7d9635733b22c1175f5ef144105def897ac818d83d47c0dea393

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyclassparser-0.8.1-py3-none-any.whl:

Publisher: release.yml on carlosplanchon/pyclassparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page