Skip to content

WIP: refactor main.py to minimal content#1894

Open
rolandwalker wants to merge 1 commit into
mainfrom
RW/minimal-main-py-refactor-20260518
Open

WIP: refactor main.py to minimal content#1894
rolandwalker wants to merge 1 commit into
mainfrom
RW/minimal-main-py-refactor-20260518

Conversation

@rolandwalker
Copy link
Copy Markdown
Contributor

Description

Creates the following:

  • mycli/cli_runner.py
  • mycli/client.py
  • mycli/client_commands.py
  • mycli/client_connection.py
  • mycli/client_query.py

and reduces main.py to only:

  • click_entrypoint()
  • main()

As first PR'd, this maintains the current testing surface. Best would be to reach the goal of individual tests over each of the above files, and outright deletion of test_main_regression.py.

Also, as first PR'd, main.py contains almost nothing. It would be logical to move the content of cli_args.py back in.

While the files are smaller and the code is more approachable, like previous refactors, this is not perfect. For example, cli_runner.py has extensive logic regarding environment variables and command-line arguments. Whereas, ideally, all configuration would be collected together in a distinct layer.

Checklist

  • I added this contribution to the changelog.md file.
  • I added my name to the AUTHORS file (or it's already there).
  • To lint and format the code, I ran
    uv run ruff check && uv run ruff format && uv run mypy --install-types .

@rolandwalker rolandwalker self-assigned this May 18, 2026
creates the following:

 * mycli/cli_runner.py
 * mycli/client.py
 * mycli/client_commands.py
 * mycli/client_connection.py
 * mycli/client_query.py

and reduces main.py to only:

 * click_entrypoint()
 * main()
@rolandwalker rolandwalker force-pushed the RW/minimal-main-py-refactor-20260518 branch from 72f7c88 to 5819fb6 Compare May 18, 2026 10:55
Comment thread test/pytests/test_main.py
echo_calls: list[str] = []
cli.echo = lambda message, **kwargs: echo_calls.append(str(message)) # type: ignore[assignment]
monkeypatch.setattr(main, 'WIN', False)
monkeypatch.setattr(mycli.compat, 'WIN', False)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should be: monkeypatch.setattr(mycli.client_connection, 'WIN', True) I believe, like in test_main_regression.py

logger = DummyLogger()
cli.logger = cast(Any, logger)
monkeypatch.setattr(main, 'WIN', True)
monkeypatch.setattr(mycli.client_connection, 'WIN', True)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is the correct one I believe; all the rest should match (this file, and test_main.py)

Comment thread mycli/client_commands.py
click.secho("No database selected", err=True, fg="red")
return

from mycli import main as main_module
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Dupe import of below with comment

Comment thread mycli/client_commands.py
except IOError as e:
return [SQLResult(status=str(e))]

from mycli import main as main_module
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Dupe import of below

Comment thread mycli/cli_runner.py

if cli_args.checkup:
main_module.main_checkup(mycli)
main_module.sys.exit(0)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For all of these sys commands, is there a reason you're accessing that through the module instead of just importing sys at the top and using that as normal?

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.

No good reason! I (and the agent) kept the tests working against main, which was awkward. This and other weird things got through due to that constraint/pattern.

The goal is to not pull anything from main; the question is whether to keep this PR open while all of that incremental work is done.

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.

2 participants