{"id":16693435,"url":"https://github.com/paulross/typin","last_synced_at":"2025-04-10T01:22:02.444Z","repository":{"id":84163344,"uuid":"105306472","full_name":"paulross/typin","owner":"paulross","description":"Python type inferencer for generating type annotations.","archived":false,"fork":false,"pushed_at":"2017-12-27T10:41:41.000Z","size":172,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T03:03:45.971Z","etag":null,"topics":["dynamic-analysis","pep484","python","type-annotations"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/paulross.png","metadata":{"files":{"readme":"README.rst","changelog":"HISTORY.rst","contributing":"CONTRIBUTING.rst","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS.rst","dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-09-29T18:50:13.000Z","updated_at":"2024-07-10T20:23:14.000Z","dependencies_parsed_at":"2023-03-01T08:00:30.780Z","dependency_job_id":null,"html_url":"https://github.com/paulross/typin","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulross%2Ftypin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulross%2Ftypin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulross%2Ftypin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulross%2Ftypin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/paulross","download_url":"https://codeload.github.com/paulross/typin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248138271,"owners_count":21053840,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dynamic-analysis","pep484","python","type-annotations"],"created_at":"2024-10-12T16:30:30.774Z","updated_at":"2025-04-10T01:22:02.422Z","avatar_url":"https://github.com/paulross.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"typin README\n============\n\n``typin`` is a *Type Inferencer* for understanding what types of objects\nare flowing through your Python code. It observes your code dynamically and can\nrecord all the types that each function sees, returns or raises.\n``typin`` can then use this information to create Python's type annotations or\n``__doc__`` strings to insert into your code.\n\n``typin`` is currently proof-of-concept and a very early prototype.\nIt is Python 3 only at the moment.\nThere is a forthcoming project https://github.com/paulross/pytest-typin which\nturns ``typin`` into a pytest plugin so that your unit tests can generate type\nannotations and documentation strings.\n\nExample\n--------\n\nLets say you have a function that creates a repeated string, like this:\n\n.. code-block:: python\n\n    def function(s, num):\n        if num \u003c 1:\n            raise ValueError('Value must be \u003e 0, not {:d}'.format(num))\n        lst = []\n        while num:\n            lst.append(s)\n            num -= 1\n        return ' '.join(lst)\n\nYou can exercise this under the watchful gaze of ``typin``:\n\n.. code-block:: python\n\n    from typin import type_inferencer\n\n    with type_inferencer.TypeInferencer() as ti:\n        assert function('Hi', 2) == 'Hi Hi'\n\nYou can then get the types that ``typin`` has observed as a string suitable for\na stub file:\n\n.. code-block:: python\n\n    ti.stub_file_str(__file__, '', 'function')\n    # returns: 'def function(s: str, num: int) -\u003e str: ...'\n\nThen adding code that provokes the exception we can track that as well:\n\n.. code-block:: python\n\n    from typin import type_inferencer\n\n    with type_inferencer.TypeInferencer() as ti:\n        assert function('Hi', 2) == 'Hi Hi' # As before\n        try:\n            function('Hi', 0)\n        except ValueError:\n            pass\n\nException specifications are not part of Python's type annotation but they are\npart of of the Sphinx documentation string standard and ``typin`` can provide that, and\nthe line number where it should be inserted:\n\n.. code-block:: python\n\n    line_number, docstring = ti.docstring(__file__, '', 'function', style='sphinx')\n    docstring\n    \"\"\"\n    \u003cinsert documentation for function\u003e\n\n    :param s: \u003cinsert documentation for argument\u003e\n    :type s: ``str``\n\n    :param num: \u003cinsert documentation for argument\u003e\n    :type num: ``int``\n\n    :returns: ``str`` -- \u003cinsert documentation for return values\u003e\n\n    :raises: ``ValueError``\n    \"\"\"\n    # Insert template docstrings into the source code.\n    new_src = ti.insert_docstrings(__file__, style='sphinx')\n    with open(__file__, 'w') as f:\n        for line in new_src:\n            f.write(line)\n\nSadly ``typin`` is not smart enough to write the documentation text for you :-)\n\nThere is a CLI interface ``typin_cli`` that is an entry point to ``typin/src/typin/typin_cli.py``.\nThis  executes arbitrary python code using ``compile()`` and ``exec()`` like the following example.\nNote use of ``--`` followed by Python script then the arguments for that script surrounded by quotes:\n\n.. code-block:: console\n\n    $ python typin_cli.py --stubs=stubs/ --write-docstrings=docstrings/ -- example.py 'foo bar baz'\n\nThis will ``compile()/exec()`` ``example.py`` with the arguments ``foo bar baz``\nwrite the stub files (``'.pyi'`` files) to ``stubs/`` and the source code with the docstrings\ninserted to ``docstrings/``.\n\n``typin_cli.py`` help:\n\n.. code-block:: console\n\n    $ python typin_cli.py --help\n    usage: typin_cli.py [-h] [-l LOGLEVEL] [-d] [-t] [-e EVENTS_TO_TRACE]\n                        [-s STUBS] [-w WRITE_DOCSTRINGS]\n                        [--docstring-style DOCSTRING_STYLE] [-r ROOT]\n                        program argstring\n\n    typin_cli - Infer types of Python functions.\n      Created by Paul Ross on 2017-10-25. Copyright 2017. All rights reserved.\n      Version: v0.1.0 Licensed under MIT License\n    USAGE\n\n    positional arguments:\n      program               Python target file to be compiled and executed.\n      argstring             Argument as a string to give to the target. Prefix\n                            this with '--' to avoid them getting consumed by\n                            typin_cli.py\n\n    optional arguments:\n      -h, --help            show this help message and exit\n      -l LOGLEVEL, --loglevel LOGLEVEL\n                            Log Level (debug=10, info=20, warning=30, error=40,\n                            critical=50) [default: 30]\n      -d, --dump            Dump results on stdout after processing. [default:\n                            False]\n      -t, --trace-frame-events\n                            Very verbose trace output, one line per frame event.\n                            [default: False]\n      -e EVENTS_TO_TRACE, --events-to-trace EVENTS_TO_TRACE\n                            Events to trace (additive). [default: []] i.e. every\n                            event.\n      -s STUBS, --stubs STUBS\n                            Directory to write stubs files. [default: ]\n      -w WRITE_DOCSTRINGS, --write-docstrings WRITE_DOCSTRINGS\n                            Directory to write source code with docstrings.\n                            [default: ]\n      --docstring-style DOCSTRING_STYLE\n                            Style of docstrings, can be: 'google', 'sphinx'.\n                            [default: sphinx]\n      -r ROOT, --root ROOT  Root path of the Python packages to generate stub\n                            files for. [default: .]\n\n\n.. image:: https://img.shields.io/pypi/v/typin.svg\n        :target: https://pypi.python.org/pypi/typin\n\n.. image:: https://img.shields.io/travis/paulross/typin.svg\n        :target: https://travis-ci.org/paulross/typin\n\n.. image:: https://readthedocs.org/projects/typin/badge/?version=latest\n        :target: https://typin.readthedocs.io/en/latest/?badge=latest\n        :alt: Documentation Status\n\n.. image:: https://pyup.io/repos/github/paulross/typin/shield.svg\n     :target: https://pyup.io/repos/github/paulross/typin/\n     :alt: Updates\n\n\nPython type inferencing.\n\n* Free software: MIT license\n* Documentation: https://typin.readthedocs.io.\n\nFeatures\n--------\n\n* TODO\n\nCredits\n---------\n\nThis package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template.\n\n.. _Cookiecutter: https://github.com/audreyr/cookiecutter\n.. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaulross%2Ftypin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpaulross%2Ftypin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaulross%2Ftypin/lists"}