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

Validating CLI arguments #531

Closed
edublancas opened this issue May 27, 2023 · 11 comments · Fixed by #561
Closed

Validating CLI arguments #531

edublancas opened this issue May 27, 2023 · 11 comments · Fixed by #561
Assignees
Labels
stash Label used to categorize issues that will be worked on next

Comments

@edublancas
Copy link

I noticed that passing random arguments to %sql doesn't raise any errors:

(jupysql)  eduardo@macbookair dev/jupysql (display *) » ipython
Python 3.10.11 (main, Apr 20 2023, 13:58:42) [Clang 14.0.6 ]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.13.2 -- An enhanced Interactive Python. Type '?' for help.

In [1]: %load_ext sql

In [2]: %sql duckdb://

In [3]: %sql --stuff --more-stuff
Running query in 'duckdb://'

In [4]:

We should throw an UsageError instead

@edublancas edublancas added stash Label used to categorize issues that will be worked on next med complexity labels May 27, 2023
@edublancas
Copy link
Author

@edublancas edublancas assigned tl3119 and unassigned tl3119 May 30, 2023
@tl3119
Copy link

tl3119 commented May 30, 2023

To solve this issue, we need to focus on the arguments part on the execute() method in magic.py, since we don't want random arguments to be passed.

@magic_arguments(allow_interspersed_args=False)
@handle_arguments
def handle_arguments(self, *args):
    if len(args) > 0:
        raise UsageError("Unrecognized argument(s): {}".format(args))

@line_magic("sql")
@cell_magic("sql")
# Existing code

In this way, we prevents unrecognized arguments from being treated as positional arguments and to handle the error we got.
Does this method works fine?
BTW, how do we test for this?

@edublancas
Copy link
Author

edublancas commented May 30, 2023

checkout the existing tests: https://github.com/ploomber/jupysql/blob/master/src/tests/test_magic.py

the solution is a bit tricky though (this project is a fork of ipython-sql so we inherited a bunch of legacy code). I forgot to put this relevant link: https://jupysql.ploomber.io/en/latest/intro.html#considerations

so for the line magic (%sql) things are a bit complicated because the same line mixes arguments with SQL comments. but I think we can start by applying this to the cell magic (%%sql), in a cell magic, there is no ambiguity between the arguments and the SQL. examples:

line magic:

%sql  [arguments go here but SQL code also goes here]

cell magic:

%%sql  [arguments go here]
[SQL code goes here]

@tl3119
Copy link

tl3119 commented May 30, 2023

So we need to deal this problem by line magic and cell magic separately. One way is to write function immediately after @line_magic("sql") and @cell_magic("sql") to indicate each function deal different situation:

@line_magic("sql")
  def check_line(self, line):
      pass
@cell_magic("sql")
  def check_cell(self, line):
      # Handle the arguments for cell_magic
      args = parse_argstring(self.check_cell, line)

      if args.unrecognized_args:
          raise UsageError("Unrecognized argument(s): {}".format(args.unrecognized_args))

Since in the cell magic, SQL code needs to be at the second line, so we can parse the first line and to check whether we get unrecognized arguments, if we we get unrecognized arguments, we raise UsageError.
Is this logic correct?

@edublancas
Copy link
Author

yeah, this makes sense: splitting into two. the root problem is that both the line and cell lines are executing the same function. but the validation logic assumptions are different (see #550)

so go ahead with the approach you suggested and we'll see how it goes!

@tl3119
Copy link

tl3119 commented May 31, 2023

Sure! I have submitted a PR request. Some tests failed, I am blocked by these tests. Do you have any suggestion for these error?

@tl3119
Copy link

tl3119 commented May 31, 2023

i have some ideas now, i will try to fix that

@edublancas
Copy link
Author

are you able to run the tests locally? that'll make things easier to debug

this will be useful: https://ploomber.io/blog/open-source/ - checkout the from IPython import embed and pdb parts.

@tl3119
Copy link

tl3119 commented May 31, 2023

I am confused with SQL comments and unrecognized arguments. Is that all the arguments should begin with "--" and I notice that "If you try to pass an unsupported argument, like --lutefisk, it will be interpreted as a SQL comment and will not throw an unsupported argument exception"
"--lutefisk" can also be interpreted as a SQL comment, so does that means we want "--lutefisk" be a unrecognized argument or it is fine to be a SQL comment? How do we distinguish between unrecognized argument and SQL comment? For example:
%sql --lutefisk SELECT ...

@edublancas
Copy link
Author

How do we distinguish between unrecognized argument and SQL comment?

we cannot do this in the line magic (%sql - single percentage sign), so we won't implement this there.

but we can do it in the cell magic (%%sql - double percentage sign) because the SQL content begins in the second line. so we can assume the first line only has arguments

@tl3119
Copy link

tl3119 commented Jun 1, 2023

Sure, that makes sense. I have submitted a new pull request. I added a new test, and all the tests pass locally in my computer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stash Label used to categorize issues that will be worked on next
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants