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

chore: Update mypy and fix stubs issue #24033

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,23 @@ repos:
hooks:
- id: isort
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.0.1
rev: v1.3.0
hooks:
- id: mypy
args: [--check-untyped-defs]
additional_dependencies: [types-all]
additional_dependencies:
[
types-simplejson,
types-python-dateutil,
types-requests,
types-redis,
types-pytz,
types-croniter,
types-PyYAML,
types-setuptools,
types-paramiko,
types-Markdown,
]
- repo: https://github.com/peterdemin/pip-compile-multi
rev: v2.6.2
hooks:
Expand Down
2 changes: 1 addition & 1 deletion superset/charts/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ def export(self, **kwargs: Any) -> Response:
buf,
mimetype="application/zip",
as_attachment=True,
attachment_filename=filename,
download_name=filename,
)
if token:
response.set_cookie(token, "done", max_age=600)
Expand Down
2 changes: 1 addition & 1 deletion superset/dashboards/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ def export(self, **kwargs: Any) -> Response:
buf,
mimetype="application/zip",
as_attachment=True,
attachment_filename=filename,
download_name=filename,
)
if token:
response.set_cookie(token, "done", max_age=600)
Expand Down
2 changes: 1 addition & 1 deletion superset/databases/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ def export(self, **kwargs: Any) -> Response:
buf,
mimetype="application/zip",
as_attachment=True,
attachment_filename=filename,
download_name=filename,
)
if token:
response.set_cookie(token, "done", max_age=600)
Expand Down
2 changes: 1 addition & 1 deletion superset/datasets/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ def export(self, **kwargs: Any) -> Response:
buf,
mimetype="application/zip",
as_attachment=True,
attachment_filename=filename,
download_name=filename,
)
if token:
response.set_cookie(token, "done", max_age=600)
Expand Down
2 changes: 2 additions & 0 deletions superset/embedded/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def embedded(
if not embedded:
abort(404)

assert embedded is not None
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mypy doc:

You can often work around these errors by using assert isinstance(obj, ClassName) or assert obj is not None to tell mypy that you know that the type is more specific than what mypy thinks.


# validate request referrer in allowed domains
is_referrer_allowed = not embedded.allowed_domains
for domain in embedded.allowed_domains:
Expand Down
2 changes: 1 addition & 1 deletion superset/extensions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def __init__(self, interval: float = 1e-4) -> None:
self.interval = interval

def init_app(self, app: Flask) -> None:
app.wsgi_app = SupersetProfiler(app.wsgi_app, self.interval) # type: ignore
app.wsgi_app = SupersetProfiler(app.wsgi_app, self.interval)


APP_DIR = os.path.join(os.path.dirname(__file__), os.path.pardir)
Expand Down
2 changes: 1 addition & 1 deletion superset/importexport/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def export(self) -> Response:
buf,
mimetype="application/zip",
as_attachment=True,
attachment_filename=filename,
download_name=filename,
)
return response

Expand Down
10 changes: 3 additions & 7 deletions superset/initialization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ def configure_middlewares(self) -> None:
CORS(self.superset_app, **self.config["CORS_OPTIONS"])

if self.config["ENABLE_PROXY_FIX"]:
self.superset_app.wsgi_app = ProxyFix( # type: ignore
self.superset_app.wsgi_app = ProxyFix(
self.superset_app.wsgi_app, **self.config["PROXY_FIX_CONFIG"]
)

Expand All @@ -606,9 +606,7 @@ def __call__(
environ["wsgi.input_terminated"] = True
return self.app(environ, start_response)

self.superset_app.wsgi_app = ChunkedEncodingFix( # type: ignore
self.superset_app.wsgi_app # type: ignore
)
self.superset_app.wsgi_app = ChunkedEncodingFix(self.superset_app.wsgi_app)

if self.config["UPLOAD_FOLDER"]:
try:
Expand All @@ -617,9 +615,7 @@ def __call__(
pass

for middleware in self.config["ADDITIONAL_MIDDLEWARE"]:
self.superset_app.wsgi_app = middleware( # type: ignore
self.superset_app.wsgi_app
)
self.superset_app.wsgi_app = middleware(self.superset_app.wsgi_app)

# Flask-Compress
Compress(self.superset_app)
Expand Down
2 changes: 1 addition & 1 deletion superset/jinja_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def url_param(
# pylint: disable=import-outside-toplevel
from superset.views.utils import get_form_data

if has_request_context() and request.args.get(param): # type: ignore
if has_request_context() and request.args.get(param):
return request.args.get(param, default)

form_data, _ = get_form_data()
Expand Down
2 changes: 1 addition & 1 deletion superset/queries/saved_queries/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def export(self, **kwargs: Any) -> Response:
buf,
mimetype="application/zip",
as_attachment=True,
attachment_filename=filename,
download_name=filename,
)
if token:
response.set_cookie(token, "done", max_age=600)
Expand Down
6 changes: 3 additions & 3 deletions superset/result_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ def stringify_values(array: NDArray[Any]) -> NDArray[Any]:
for obj in it:
if na_obj := pd.isna(obj):
# pandas <NA> type cannot be converted to string
obj[na_obj] = None # type: ignore
obj[na_obj] = None
else:
try:
# for simple string conversions
# this handles odd character types better
obj[...] = obj.astype(str) # type: ignore
obj[...] = obj.astype(str)
except ValueError:
obj[...] = stringify(obj) # type: ignore
obj[...] = stringify(obj)

return result

Expand Down
4 changes: 2 additions & 2 deletions superset/sqllab/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def _create_sql_json_command(
)
execution_context_convertor = ExecutionContextConvertor()
execution_context_convertor.set_max_row_in_display(
int(config.get("DISPLAY_MAX_ROW")) # type: ignore
int(config.get("DISPLAY_MAX_ROW"))
)
return ExecuteSqlCommand(
execution_context,
Expand All @@ -359,7 +359,7 @@ def _create_sql_json_executor(
sql_json_executor = SynchronousSqlJsonExecutor(
query_dao,
get_sql_results,
config.get("SQLLAB_TIMEOUT"), # type: ignore
config.get("SQLLAB_TIMEOUT"),
is_feature_enabled("SQLLAB_BACKEND_PERSISTENCE"),
)
return sql_json_executor
4 changes: 1 addition & 3 deletions superset/sqllab/query_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ def _validate(
if is_feature_enabled("ENABLE_TEMPLATE_PROCESSING"):
# pylint: disable=protected-access
syntax_tree = sql_template_processor._env.parse(rendered_query)
undefined_parameters = find_undeclared_variables( # type: ignore
syntax_tree
)
undefined_parameters = find_undeclared_variables(syntax_tree)
if undefined_parameters:
self._raise_undefined_parameter_exception(
execution_context, undefined_parameters
Expand Down
2 changes: 1 addition & 1 deletion superset/utils/encrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __init__(self) -> None:

def init_app(self, app: Flask) -> None:
self._config = app.config
self._concrete_type_adapter = self._config[
self._concrete_type_adapter = self._config[ # type: ignore
"SQLALCHEMY_ENCRYPTED_FIELD_TYPE_ADAPTER"
]()

Expand Down
4 changes: 2 additions & 2 deletions superset/utils/pandas_postprocessing/boxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def whisker_low(series: Series) -> float:
return np.nanpercentile(series, low)

else:
whisker_high = np.max # type: ignore
whisker_low = np.min # type: ignore
whisker_high = np.max
whisker_low = np.min

def outliers(series: Series) -> Set[float]:
above = series[series > whisker_high(series)]
Expand Down
2 changes: 1 addition & 1 deletion superset/utils/url_map_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class RegexConverter(BaseConverter):
def __init__(self, url_map: Map, *items: List[str]) -> None:
super().__init__(url_map) # type: ignore
super().__init__(url_map)
self.regex = items[0]


Expand Down
8 changes: 4 additions & 4 deletions superset/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ def show_http_exception(ex: HTTPException) -> FlaskResponse:
and ex.code in {404, 500}
):
path = resource_filename("superset", f"static/assets/{ex.code}.html")
return send_file(path, cache_timeout=0), ex.code
return send_file(path, max_age=0), ex.code

return json_errors_response(
errors=[
Expand All @@ -512,7 +512,7 @@ def show_command_errors(ex: CommandException) -> FlaskResponse:
logger.warning("CommandException", exc_info=True)
if "text/html" in request.accept_mimetypes and not config["DEBUG"]:
path = resource_filename("superset", "static/assets/500.html")
return send_file(path, cache_timeout=0), 500
return send_file(path, max_age=0), 500

extra = ex.normalized_messages() if isinstance(ex, CommandInvalidError) else {}
return json_errors_response(
Expand All @@ -534,7 +534,7 @@ def show_unexpected_exception(ex: Exception) -> FlaskResponse:
logger.exception(ex)
if "text/html" in request.accept_mimetypes and not config["DEBUG"]:
path = resource_filename("superset", "static/assets/500.html")
return send_file(path, cache_timeout=0), 500
return send_file(path, max_age=0), 500

return json_errors_response(
errors=[
Expand Down Expand Up @@ -733,7 +733,7 @@ def apply_http_headers(response: Response) -> Response:
"""Applies the configuration's http headers to all responses"""

# HTTP_HEADERS is deprecated, this provides backwards compatibility
response.headers.extend( # type: ignore
response.headers.extend(
{**config["OVERRIDE_HTTP_HEADERS"], **config["HTTP_HEADERS"]}
)

Expand Down
6 changes: 4 additions & 2 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,8 @@ def dashboard(
if not dashboard:
abort(404)

assert dashboard is not None

has_access_ = False
for datasource in dashboard.datasources:
datasource = DatasourceDAO.get_datasource(
Expand Down Expand Up @@ -2419,7 +2421,7 @@ def _create_sql_json_command(
)
execution_context_convertor = ExecutionContextConvertor()
execution_context_convertor.set_max_row_in_display(
int(config.get("DISPLAY_MAX_ROW")) # type: ignore
int(config.get("DISPLAY_MAX_ROW"))
)
return ExecuteSqlCommand(
execution_context,
Expand All @@ -2444,7 +2446,7 @@ def _create_sql_json_executor(
sql_json_executor = SynchronousSqlJsonExecutor(
query_dao,
get_sql_results,
config.get("SQLLAB_TIMEOUT"), # type: ignore
config.get("SQLLAB_TIMEOUT"),
is_feature_enabled("SQLLAB_BACKEND_PERSISTENCE"),
)
return sql_json_executor
Expand Down
4 changes: 2 additions & 2 deletions superset/views/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def get_form_data( # pylint: disable=too-many-locals
) -> Tuple[Dict[str, Any], Optional[Slice]]:
form_data: Dict[str, Any] = initial_form_data or {}

if has_request_context(): # type: ignore
if has_request_context():
# chart data API requests are JSON
request_json_data = (
request.json["queries"][0]
Expand Down Expand Up @@ -186,7 +186,7 @@ def get_form_data( # pylint: disable=too-many-locals
json_data = form_data["queries"][0] if "queries" in form_data else {}
form_data.update(json_data)

if has_request_context(): # type: ignore
if has_request_context():
url_id = request.args.get("r")
if url_id:
saved_url = db.session.query(models.Url).filter_by(id=url_id).first()
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/reports/scheduler_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import pytest
from flask_appbuilder.security.sqla.models import User
from freezegun import freeze_time
from freezegun.api import FakeDatetime # type: ignore
from freezegun.api import FakeDatetime

from superset.extensions import db
from superset.reports.models import ReportScheduleType
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/tasks/test_cron_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import pytz
from dateutil import parser
from freezegun import freeze_time
from freezegun.api import FakeDatetime # type: ignore
from freezegun.api import FakeDatetime

from superset.tasks.cron_util import cron_schedule_window

Expand Down