-
Notifications
You must be signed in to change notification settings - Fork 14.3k
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
chore: upgrade mypy and add type guards #16227
Changes from all commits
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 |
---|---|---|
|
@@ -29,7 +29,7 @@ | |
|
||
from flask import Flask | ||
from flask_caching import Cache | ||
from typing_extensions import TypedDict | ||
from typing_extensions import Literal, TypedDict | ||
from werkzeug.wrappers import Response | ||
|
||
if TYPE_CHECKING: | ||
|
@@ -57,7 +57,7 @@ class AdhocMetricColumn(TypedDict, total=False): | |
class AdhocMetric(TypedDict, total=False): | ||
aggregate: str | ||
column: Optional[AdhocMetricColumn] | ||
expressionType: str | ||
expressionType: Literal["SIMPLE", "SQL"] | ||
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 guess this is screaming out to be an enum. Definitely a future TODO. 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. Good idea. In the next iteration I'll see if I can convert some of these |
||
label: Optional[str] | ||
sqlExpression: Optional[str] | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,7 +81,7 @@ | |
from sqlalchemy.engine.reflection import Inspector | ||
from sqlalchemy.sql.type_api import Variant | ||
from sqlalchemy.types import TEXT, TypeDecorator, TypeEngine | ||
from typing_extensions import TypedDict | ||
from typing_extensions import TypedDict, TypeGuard | ||
|
||
import _thread # pylint: disable=C0411 | ||
from superset.constants import ( | ||
|
@@ -1275,7 +1275,7 @@ def backend() -> str: | |
return get_example_database().backend | ||
|
||
|
||
def is_adhoc_metric(metric: Metric) -> bool: | ||
def is_adhoc_metric(metric: Metric) -> TypeGuard[AdhocMetric]: | ||
return isinstance(metric, dict) and "expressionType" in metric | ||
|
||
|
||
|
@@ -1288,7 +1288,6 @@ def get_metric_name(metric: Metric) -> str: | |
:raises ValueError: if metric object is invalid | ||
""" | ||
if is_adhoc_metric(metric): | ||
metric = cast(AdhocMetric, metric) | ||
label = metric.get("label") | ||
if label: | ||
return label | ||
|
@@ -1306,7 +1305,7 @@ def get_metric_name(metric: Metric) -> str: | |
if column_name: | ||
return column_name | ||
raise ValueError(__("Invalid metric object")) | ||
return cast(str, metric) | ||
return metric # type: ignore | ||
Comment on lines
-1309
to
+1308
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. For some reason |
||
|
||
|
||
def get_metric_names(metrics: Sequence[Metric]) -> List[str]: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the cleanest solution I could come up with to automatically populate stubs for mypy pre-commit hooks: pre-commit-ci/issues#69 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@villebro this is what we did at Airbnb as well. It's a real shame that Mypy doesn't install these by default if needed.