Skip to main content

Python Code Parser

Simple functions built on Python ast to parse Python code and extract information about functions, parameters, etc.

Used for auto-type-hinting projects.

Install with pip

pip install python_code_parse

Functions

get_function_info_by_name

Returns a FunctionInfo object containing information about a function and it's parameters in a given code string.

get_all_function_info_from_code

Returns a list of FunctionInfo objects, each containing information about a function and it's parameters in a given code string.

replace_function_signature

Replace a function declaration including the parameters (with annotations and default values) and the return type.

function_returns

Returns a boolean value saying if a given function returns a value or None.

Example

Below is the output of running the parser on the example examples/basic_example.py:

from python_code_parse import get_all_function_info_from_code, FunctionInfo
from typing import List

with open("examples/basic_example.py", "r") as f:
    data = f.read()

function_infos: List[FunctionInfo] = get_all_function_info_from_code(data)

print(function_infos)

"""
[
  FunctionInfo(
    name='sum',
    args=[
      FunctionArg(name='a', annotation='int'),
      FunctionArg(name='b', annotation='', default = '1')
    ],
    return_type='None',
    line=1
  ),
  FunctionInfo(
    name='subtract',
    args=[
      FunctionArg(name='a', annotation=''),
      FunctionArg(name='b', annotation='')
    ],
    return_type='int',
    line=5
  ),
  FunctionInfo(
    name='log',
    args=[
      FunctionArg(name='message', annotation='str', special=SpecialArg.kwonlyargs),
    ],
    return_type='None',
    line=9
  )
]
"""

Metadata

Release files for python-code-parse 0.0.14

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for python-code-parse 0.0.14
File Interpreter ABI Platform
python_code_parse-0.0.14-py2.py3-none-any.whl Python 3, Python 2 none any Details

Release files / python_code_parse-0.0.14-py2.py3-none-any.whl

Download URL python_code_parse-0.0.14-py2.py3-none-any.whl
Size 9.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
497d5b5adc339df629a48c6db30e27ca34334e90191f0ba06963d0677ec182d3
BLAKE2b-256 checksum
How to use checksums
d3b1f12eaa96c8f7dda2b535910ae3322ce431ea50440af864cf0c250ed9cc7a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.9

Release history Release notifications | RSS feed

This release

0.0.14 This release

1 release file

0.0.13

1 release file

0.0.12

1 release file

0.0.11

1 release file

0.0.10

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page