Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: factorise plugins app implementation #1166

Merged
merged 27 commits into from
Jun 11, 2024

Conversation

sfc-gh-pczajka
Copy link
Collaborator

@sfc-gh-pczajka sfc-gh-pczajka commented Jun 6, 2024

Pre-review checklist

  • I've confirmed that instructions included in README.md are still correct after my changes in the codebase.
  • I've added or updated automated unit tests to verify correctness of my new code.
  • I've added or updated integration tests to verify correctness of my new code.
  • I've confirmed that my changes are working by executing CLI's commands manually.
  • I've confirmed that my changes are up-to-date with the target branch.
  • I've described my changes in the release notes.
  • I've described my changes in the section below.

Changes description

Refactor: change SnowTyper into Typer instance fanctory.
Only developer-facing change: we need to call SnowTyper.create_app() to get an actual Typer instance.

The motivation behind the change is an issue #813, which was caused by reading config.toml before error handling was set up

@sfc-gh-pczajka sfc-gh-pczajka requested review from a team as code owners June 6, 2024 11:38
@@ -55,7 +64,7 @@ def _mask_password(connection_params: dict):
return connection_params


@app.command(name="list")
@app_creator.command(name="list")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Using app will reduce a lot of changes, I don't think we need to to the rename

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

changed

Comment on lines 36 to 43
class ConnectionAppCreator(SnowTyperCreator):
def create_app(self) -> SnowTyper:
app = SnowTyper(
name="connection",
help="Manages connections to Snowflake.",
)
self.register_commands(app)
return app
Copy link
Collaborator

@sfc-gh-turbaszek sfc-gh-turbaszek Jun 6, 2024

Choose a reason for hiding this comment

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

Can't we simplify it to something like

app: SnowTyperCreator = SnowTyperCreator.create_app(SnowTyper(
            name="connection",
            help="Manages connections to Snowflake.",
        ))

do we really need a custom class for every command group?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I could pass **kwargs from create_app to the constructor, but then I'd have to modify add_init_command-like procedures to work with SnowTyperCreator - I will check

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

changed as discussed

Comment on lines 148 to 168
def __init__(self):
self.commands_to_register: List[SnowTyperCommandData] = []

def create_app(self) -> SnowTyper:
"""
Returns SnowTyper instance.
"""
raise NotImplementedError()

def command(self, *args, **kwargs):
def decorator(command):
self.commands_to_register.append(
SnowTyperCommandData(command, args=args, kwargs=kwargs)
)
return command

return decorator

def register_commands(self, app: SnowTyper) -> None:
for command in self.commands_to_register:
app.command(*command.args, **command.kwargs)(command.func)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why we cannot add this logic to SnowTyper? What's reasoning behind adding this additional layer?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We want to postpone calling __init__ until error handling would be set up. The problem behind the issue was calling feature flags (which read config.toml) to determine constructor arguments (hidden=FeatureFlag.is_disabled)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

changed as discussed

@sfc-gh-pczajka sfc-gh-pczajka marked this pull request as draft June 6, 2024 15:12
@sfc-gh-pczajka sfc-gh-pczajka changed the title Snow 1064130 refactor plugins to app factory Refactor: factorise plugins app implementation Jun 6, 2024
@sfc-gh-pczajka sfc-gh-pczajka marked this pull request as ready for review June 6, 2024 16:26
kwargs: Dict[str, Any]


class SnowTyper:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we name it SnowTyperFactory?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

renamed:
SnowTyper -> SnowTyperFactory
SnowTyperInstance -> SnowTyper
.create_app() -> .create_instance()

Copy link
Contributor

Choose a reason for hiding this comment

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

  • src/snowflake/cli/plugins/nativeapp/version/commands.py also needs these changes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is already changed :)

)

app.add_typer(stage_app) # type: ignore
app.add_typer(show_app) # type: ignore
Copy link
Collaborator

Choose a reason for hiding this comment

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

why?

Copy link
Collaborator Author

@sfc-gh-pczajka sfc-gh-pczajka Jun 10, 2024

Choose a reason for hiding this comment

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

I made it consistent with other plugins (spcs / NADE), but I made the choice between __init__.py and commands.py arbitrarily - no strong opinion

╰──────────────────────────────────────────────────────────────────────────────╯

'''
# ---
Copy link
Collaborator

Choose a reason for hiding this comment

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

🚀

sfc-gh-bgoel
sfc-gh-bgoel previously approved these changes Jun 10, 2024
Copy link
Contributor

@sfc-gh-bgoel sfc-gh-bgoel left a comment

Choose a reason for hiding this comment

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

lgtm from NADE

Copy link
Contributor

@sfc-gh-bgoel sfc-gh-bgoel left a comment

Choose a reason for hiding this comment

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

LGTM, acking the rebase.

@sfc-gh-pczajka sfc-gh-pczajka enabled auto-merge (squash) June 10, 2024 20:35
@sfc-gh-pczajka sfc-gh-pczajka merged commit 1cde2d0 into main Jun 11, 2024
24 checks passed
@sfc-gh-pczajka sfc-gh-pczajka deleted the SNOW-1064130-refactor-plugins-to-app-factory branch June 11, 2024 08:43
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.

None yet

5 participants