Repository navigation
Fetch storage_id once per Repository #10586
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
base: master
Are you sure you want to change the base?
Changes from 5 commits
1f91c36
db78e93
a7715c5
ab0410c
c932caf
fb44f23
73fbd47
22aacd5
77e28bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| """ | ||
| Module for renaming kwargs | ||
|
|
||
| Based on https://stackoverflow.com/a/74564394 but does not warn. | ||
| """ | ||
|
|
||
| from functools import wraps | ||
|
|
||
|
|
||
| def renamed_parameters(**kwarg_map): | ||
| """ | ||
| Decorator for renaming args | ||
|
|
||
| To rename old1 -> new1 and old2 -> new2, decorate the function definition: | ||
|
|
||
| @renamed_parameters(old1="new1", old2="new2"}) | ||
| def func(new1, new2): | ||
| pass | ||
| """ | ||
| def decorator(func): | ||
| @wraps(func) | ||
| def wrapped(*args, **kwargs): | ||
| new_kwargs = {} | ||
| for k, v in kwargs.items(): | ||
| new_kwargs[kwarg_map.get(k, k)] = v | ||
| return func(*args, **new_kwargs) | ||
| return wrapped | ||
| return decorator | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add type-hints to preserve type signature for all the decorated functions/constructors? Note that this will start raising typecheck errors for Also, would love to hear your response for #10586 (comment) comment. :) It would look something like follows: from collections.abc import Callable
from functools import wraps
from typing import ParamSpec, TypeVar, cast
P = ParamSpec("P")
R = TypeVar("R")
def renamed_parameters(**kwarg_map: str) -> Callable[[Callable[P, R]], Callable[P, R]]:
"""
Decorator for renaming args
To rename old1 -> new1 and old2 -> new2, decorate the function definition:
@renamed_parameters(old1="new1", old2="new2")
def func(new1, new2):
pass
"""
def decorator(func: Callable[P, R]) -> Callable[P, R]:
@wraps(func)
def wrapped(*args: P.args, **kwargs: P.kwargs) -> R:
new_kwargs = {}
for k, v in kwargs.items():
new_kwargs[kwarg_map.get(k, k)] = v
return cast(Callable[..., R], func)(*args, **new_kwargs)
return wrapped
return decorator
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not want to start raising errors for repository_id... yet.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also I am not sure what this actually changes. wrapped has the same type as func. Indeed, lsp-pyright gives the correct hint.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, mypy does not do type-inference, and loses those information. # script.py
from lakefs._decorators import renamed_parameters
@renamed_parameters(repository_id="repo")
def get_repo(repo: str) -> str:
return repo
get_repo(repo=123)
get_repo(repository_id="my-repo")With the above content: $ mypy script.py
Success: no issues found in 1 source file$ pyright script.py
pyright script.py
/Users/saugat/projects/treeverse/lakeFS/clients/python-wrapper/script.py
/Users/saugat/projects/treeverse/lakeFS/clients/python-wrapper/script.py:7:15 - error: Argument of type "Literal[123]" cannot be assigned to parameter "repo" of type "str" in function "get_repo"
"Literal[123]" is not assignable to "str" (reportArgumentType)
/Users/saugat/projects/treeverse/lakeFS/clients/python-wrapper/script.py:8:1 - error: Argument missing for parameter "repo" (reportCallIssue)
/Users/saugat/projects/treeverse/lakeFS/clients/python-wrapper/script.py:8:10 - error: No parameter named "repository_id" (reportCallIssue)
3 errors, 0 warnings, 0 informations$ ty check script.py
All checks passed!So, Note that if you do add typehints to the decorator, then all 3 of them will start raising typechecker errors for users using |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| """ | ||
| Module containing str-or-Object getters | ||
|
|
||
| This can help break import cycles. To convert Repository | str to | ||
| Repository in another client module you can | ||
|
|
||
| from typing import TYPE_CHECKING | ||
| if TYPE_CHECKING: | ||
| from repository import Repository | ||
| from getter import repository_for | ||
|
|
||
| repo: Repository = repository_for('abc') | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
| from typing import Optional, TYPE_CHECKING | ||
| from lakefs.client import Client | ||
| # Import all of repository to break circular import. | ||
| from lakefs import repository as repository_mod # Rename: repository is also a function. | ||
|
|
||
|
|
||
| def repository_for(repo: repository_mod.Repository | str, client: Optional[Client]) -> repository_mod.Repository: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A good alternative would be to import
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would still need to define repository_for, though. So improving documentation to clear that up. |
||
| return repository_mod.Repository(repo, client) if isinstance(repo, str) else repo | ||
Uh oh!
There was an error while loading. Please reload this page.