refactor: Improve typing annotations for planner, schema, serde, and transforms modules#7579
refactor: Improve typing annotations for planner, schema, serde, and transforms modules#7579OutSquareCapital wants to merge 5 commits intotobymao:mainfrom
Conversation
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
| ] = [] # final selects in this chain of steps representing a select | ||
| operands = {} # intermediate computations of agg funcs eg x + 1 in SUM(x + 1) | ||
| aggregations = {} | ||
| # final selects in this chain of steps representing a select |
There was a problem hiding this comment.
Took the liberty to move the comment on the line above because the additional type hints made this very unreadable
| if node.args: | ||
| for k, vs in reversed(node.args.items()): | ||
| if type(vs) is list: | ||
| if isinstance(vs, list): |
There was a problem hiding this comment.
type(x) is y is legacy pattern and doesn't work as well on type checkers than isinstance
| return payloads | ||
|
|
||
|
|
||
| def _has_parent(node: object) -> TypeIs[exp.Expr]: |
There was a problem hiding this comment.
This helper was the best option to type this correctly
|
|
||
| select_candidates = exp.Window if expression.is_star else (exp.Window, exp.Column) | ||
| for select_candidate in list(qualify_filters.find_all(select_candidates)): | ||
| select_candidates = (exp.Window,) if expression.is_star else (exp.Window, exp.Column) |
There was a problem hiding this comment.
This was a potential bug: we could pass a tuple as an argument, but we were not unpacking it afterwards.
| select_candidates = exp.Window if expression.is_star else (exp.Window, exp.Column) | ||
| for select_candidate in list(qualify_filters.find_all(select_candidates)): | ||
| select_candidates = (exp.Window,) if expression.is_star else (exp.Window, exp.Column) | ||
| for select_candidate in qualify_filters.find_all(*select_candidates): |
There was a problem hiding this comment.
collecting in a list here served no purpose and was a performance tax
| alias = join_expr.args.get("alias") | ||
| else: | ||
| alias = unnest.args.get("alias") | ||
| if alias is None: |
There was a problem hiding this comment.
This had to be done because the only None check that was done on alias was for the alias_col variable.
So later on with the alias.this attribute access, this could crash, and mypy wasn't letting this pass anyways
| *[ | ||
| exp.column(col, tables[0]).eq(exp.column(col, tables[1])) | ||
| for col in full_outer_join.args.get("using") | ||
| for col in t.cast(list[exp.Identifier], full_outer_join.args.get("using")) |
There was a problem hiding this comment.
Cast is needed here, unless we extract this path in a function helper to correctly type the full_outer_join.args.get("using")
| return exp.column(alias_or_name, quoted=identifier.args.get("quoted")) | ||
| return alias_or_name | ||
|
|
||
| outer_selects = exp.select(*list(map(_select_alias_or_name, expression.selects))) |
There was a problem hiding this comment.
collecting in a list here served no purpose
|
mypy don't fail in local for me. I see that there have been recent changes regarding mypy usage in the repo, could this be linked @VaggelisD ? |
|
Could be, can you try to reset the venv and install dependencies again? |
to be honest I always use uvx for ruff and mypy instead of uv run, i.e the venv in this codebase, due to repeated issues I had with uv, not having Anyway, I just tested, it used to work with 3.9, but not anymore. Still work fine with 3.10 tho. But I suspect now uvx target 3.10 instead of 3.9 version for mypy in consequence, so I can't see issues with this specific version. PS C:\Users\tibo\python_codes\sqlglot> uv sync --python 3.9
Using CPython 3.9.25
Creating virtual environment at: .venv
× Failed to build `sqlglotc @
│ file:///C:/Users/tibo/python_codes/sqlglot/sqlglotc`
├─▶ Failed to resolve requirements from `build-system.requires`
│ `setuptools-scm`, `sqlglot-mypy>=1.20.0.post4`,
│ `types-python-dateutil`, `sqlglot`
╰─▶ Because only the following versions of sqlglot-mypy are
available:
sqlglot-mypy<=1.20.0.post4
sqlglot-mypy==1.20.1.post1
and sqlglot-mypy==1.20.1.post1 was yanked (reason: It
should have been 1.20.0.post1), we can conclude that
Because the current Python version (3.9.25) does not satisfy
Python>=3.10, we can conclude that sqlglot-mypy==1.20.0.post4
cannot be used.
And because we know from (1) that sqlglot-mypy>1.20.0.post4
cannot be used, we can conclude that
sqlglot-mypy>=1.20.0.post4 cannot be used.
conclude that your requirements are unsatisfiable.
PS C:\Users\tibo\python_codes\sqlglot> uv sync --python 3.10
Using CPython 3.10.19
Removed virtual environment at: .venv
Creating virtual environment at: .venv
Resolved 58 packages in 2m 47s
Built sqlglot @ file:///C:/Users/tibo/python_codes/sqlglot
Prepared 1 package in 5.20s
Installed 1 package in 28ms
+ sqlglot==0.0.1.dev7554 (from file:///C:/Users/tibo/python_codes/sqlglot)EDIT: PS C:\Users\tibo\python_codes\sqlglot> uvx --python 3.9 mypy sqlglot/
Installed 6 packages in 2.96s
sqlglot\optimizer\simplify.py:20: error: Library stubs not installed for "dateutil.relativedelta" [import-untyped]
sqlglot\optimizer\simplify.py:20: note: Hint: "python3 -m pip install types-python-dateutil"
sqlglot\optimizer\simplify.py:20: note: (or run "mypy --install-types" to install all missing stub packages)
sqlglot\optimizer\simplify.py:20: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 175 source files)
PS C:\Users\tibo\python_codes\sqlglot> |
|
Hey @OutSquareCapital, for context, Mypy 1.20 (latest release) dropped support for Python 3.9 and so did we for
As for the CI failures, I think they're justified (?). Afaict, it's rejecting |
Co-authored-by: Copilot <copilot@github.com>
|
Okay apologies to both of you guys, switching branches again gave me the same errors than in the CI. |
|
@OutSquareCapital Nothing to worry about! I also feel like I get cryptic mypy cache issues from time to time, so either that makes 2 of us or it's bound to happen, especially with the pyproject change. Not sure what the new CI failure is about, can you repro that locally? |
|
I don't have the same output but running unittest as it is will with |
|
What's the status here? |
I will try to fix this tomorrow if I have some time or sunday, will update once fixed |
Another day, another typing PR!
This one focus more on functions/methods bodies, since the signatures are now well covered.
A lot of variables where implicitly
Anydue toExpr::args::getusage.A few non-typing changes had to be made, I explain why in comments.
unrelated, IDK why copilot keep co-authoring my commits, I 100% assume using AI heavily but NOT for typing work. Didn't even prompted him once. Maybe autocomplete, idk