Skip to content

Refactor main.py to minimal content#1894

Merged
rolandwalker merged 1 commit into
mainfrom
RW/minimal-main-py-refactor-20260518
May 30, 2026
Merged

Refactor main.py to minimal content#1894
rolandwalker merged 1 commit into
mainfrom
RW/minimal-main-py-refactor-20260518

Conversation

@rolandwalker
Copy link
Copy Markdown
Contributor

@rolandwalker rolandwalker commented May 18, 2026

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. Edit: keeping the structure as-is for this PR, and looking to followups to resolve.

Also, as first PR'd, main.py contains almost nothing. It would be logical to move the content of cli_args.py back in. Edit: keeping the structure as-is for this PR, and looking to followups to resolve.

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
@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 Outdated
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 Outdated

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.

@rolandwalker rolandwalker force-pushed the RW/minimal-main-py-refactor-20260518 branch from 5819fb6 to 22eef14 Compare May 30, 2026 14:16
@rolandwalker rolandwalker changed the title WIP: refactor main.py to minimal content Refactor main.py to minimal content May 30, 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 22eef14 to 14a53c7 Compare May 30, 2026 14:21
@rolandwalker
Copy link
Copy Markdown
Contributor Author

Even though there are some obvious followups wanted, I've convinced myself to merge this as-is (after accounting for review comments). It may be valuable to have the steps of refactoring in the commit history.

@rolandwalker rolandwalker merged commit 9476a3d into main May 30, 2026
8 checks passed
@rolandwalker rolandwalker deleted the RW/minimal-main-py-refactor-20260518 branch May 30, 2026 14:28
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