Skip to content

Gradual typing - #1217

Open
akaihola wants to merge 12 commits into
Aider-AI:mainfrom
akaihola:gradual-typing
Open

Gradual typing#1217
akaihola wants to merge 12 commits into
Aider-AI:mainfrom
akaihola:gradual-typing

Conversation

@akaihola

@akaihola akaihola commented Aug 29, 2024

Copy link
Copy Markdown
Contributor

Here's another stab at adding typing since #639 was closed.

I acknowledge that Paul has expressed that he isn't currently planning to add type hints, and that reviewing type hints would be a burden.

However, I believe this extremely minimal Mypy configuration and a minimal set of changes not only make Mypy pass, but also enable to use it to check some types, and allow development to continue without requiring full type hints everywhere.

Mypy takes over the burden of reviewing type annotations from humans.

Most notably, functions with no type hints are not checked by Mypy at all with this configuration. This allows adding type hints just only to select sections of the code base. It is still of course possible to gradually add typing overall and increase Mypy's strictness if desired.

See Using mypy with an existing codebase for more information.

)

repo_content_prefix = """Here are summaries of some files present in my git repository.
repo_content_prefix: str | None = """Here are summaries of some files present in my git repository.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type union syntax X | Y is only available in Python 3.10, but Aider still supports Python 3.9.
Instead, this should be

Suggested change
repo_content_prefix: str | None = """Here are summaries of some files present in my git repository.
repo_content_prefix: typing.Optional[str] = """Here are summaries of some files present in my git repository.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 1 of this file, I've added

from __future__ import annotations

which makes sure X | Y is supported on Python 3.9.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to put that in the tests too it looks like:

TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'

@nedtwigg

Copy link
Copy Markdown

I would think that typing an untyped python codebase is exactly the kind of thing that Aider itself would be great at. Typing the edges is trivial, that leads to type errors, fix those, that leads to new type errors, fix those, etc.

I also think if it's worth adding type annotations at all, then it's worth bumping to a Python version that supports the latest / greatest versions of them. Aider seems like more of an end-user thing than infrastructure library, seems okay for it to be more aggressive in bumping requirements than a typical library.

@Faolain

Faolain commented Sep 22, 2024

Copy link
Copy Markdown

Amazing work as this will make aider improving itself even better (from experience llms do quite well with typed code)

@akaihola
akaihola force-pushed the gradual-typing branch 4 times, most recently from 0f49a3d to 6416497 Compare October 5, 2024 19:15
@CLAassistant

CLAassistant commented Oct 16, 2024

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@akaihola
akaihola force-pushed the gradual-typing branch 2 times, most recently from 1a50afe to 77438bb Compare December 29, 2024 20:54
@akaihola

Copy link
Copy Markdown
Contributor Author

I rebased on main and fixed all typing errors which had appeared meanwhile. Also added a requirements file for running Mypy. It includes Mypy itself and some type stub packages. The workflow now uses this requirements file as well as other requirements files needed for type checking.

@ryanpeach

ryanpeach commented Jan 22, 2025

Copy link
Copy Markdown

Have you considered pyright instead? Pyright is fast enough to work in your vscode and neovim live, and can go in your pre-commit.

Also we should put typing into the github actions.

Personally I think typing will make aider better both in scripting mode (making it more understandable to devs, less likely for bugs) and for self-modification via aider (it will improve the linting loop, and will give more informative repo maps).

I'd definitely love to see a blog post about the accuracy of aider edits under properly typed vs untyped code generation.

@ryanpeach ryanpeach left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minimal IMO. I like it (though I have no authority)

Comment thread .github/workflows/lint.yml
@ryanpeach ryanpeach mentioned this pull request Jan 22, 2025

@ryanpeach ryanpeach left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few more lint comments

Comment thread .github/workflows/lint.yml Outdated
name: Linting

on:
push:

@ryanpeach ryanpeach Jan 22, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why we need this on push, just PR right?

- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: "3.12"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a matrix like in the other workflows

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the benefit of running Mypy on other Python versions than the most recent supported one?

@ryanpeach

Copy link
Copy Markdown
ERROR: Cannot install None, aider-chat and aider-chat[browser,dev,mypy,playwright]==0.1.dev1+g1aa8de2 because these package versions have conflicting dependencies.

The conflict is caused by:
    aider-chat 0.1.dev1+g1aa8de2 depends on attrs==24.3.0
    aiohttp 3.11.11 depends on attrs>=17.3.0
    jsonschema 4.23.0 depends on attrs>=22.2.0
    referencing 0.36.0 depends on attrs>=22.2.0
    aider-chat[browser,dev,mypy,playwright] 0.1.dev1+g1aa8de2 depends on attrs==24.3.0
    aider-chat[browser,dev,mypy,playwright] 0.1.dev1+g1aa8de2 depends on attrs==24.3.0; extra == "browser"
    aider-chat[browser,dev,mypy,playwright] 0.1.dev1+g1aa8de2 depends on attrs==24.2.0; extra == "mypy"

To fix this you could try to:
1. loosen the range of package versions you've specified
2. remove package versions to allow pip to attempt to solve the dependency conflict

https://github.com/OnScale/aider-stateless/actions/runs/12914071198/job/36012781606?pr=7

@ryanpeach

Copy link
Copy Markdown

I've noticed a lot of dead code, dead variables, etc in the codebase.

I think this is from aider writing its own code a lot.

I think if we are going to have aider write its own code, strong linting is a necessity

@Faolain

Faolain commented Feb 11, 2025

Copy link
Copy Markdown

Completely agree here @paul-gauthier what are your thoughts?

@akaihola
akaihola force-pushed the gradual-typing branch 2 times, most recently from 4c25a36 to c74b2d1 Compare February 22, 2025 08:06
Comment thread aider/exceptions.py
name: str
retry: bool
description: str
description: str | None

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to add from __future__ import annotations to the top of this file.

akaihola added 4 commits July 16, 2025 23:03
- skip some directories
- skip 3rd party packages without typing or type stubs
- allow untyped globals and class attributes
- don't check untyped functions and methods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants