Skip to content

Commit

Permalink
chore: Update mypy and fix stubs issue (#24033)
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneTorap authored and eschutho committed Dec 2, 2023
1 parent 056c598 commit b76aa57
Show file tree
Hide file tree
Showing 34 changed files with 90 additions and 83 deletions.
17 changes: 15 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,23 @@ repos:
hooks:
- id: isort
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.941
rev: v1.3.0
hooks:
- id: mypy
additional_dependencies: [types-all]
args: [--check-untyped-defs]
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.4.1
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 @@ -780,7 +780,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 @@ -803,7 +803,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
8 changes: 3 additions & 5 deletions superset/databases/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,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 Expand Up @@ -1263,12 +1263,10 @@ def available(self) -> Response:
and hasattr(engine_spec, "sqlalchemy_uri_placeholder")
and getattr(engine_spec, "default_driver") in drivers
):
payload[
"parameters"
] = engine_spec.parameters_json_schema() # type: ignore
payload["parameters"] = engine_spec.parameters_json_schema()
payload[
"sqlalchemy_uri_placeholder"
] = engine_spec.sqlalchemy_uri_placeholder # type: ignore
] = engine_spec.sqlalchemy_uri_placeholder

available_databases.append(payload)

Expand Down
5 changes: 2 additions & 3 deletions superset/databases/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,15 @@ def build_sqlalchemy_uri(
)

# validate parameters
parameters = engine_spec.parameters_schema.load(parameters) # type: ignore
parameters = engine_spec.parameters_schema.load(parameters)

serialized_encrypted_extra = data.get("masked_encrypted_extra") or "{}"
try:
encrypted_extra = json.loads(serialized_encrypted_extra)
except json.decoder.JSONDecodeError:
encrypted_extra = {}

data["sqlalchemy_uri"] = engine_spec.build_sqlalchemy_uri( # type: ignore
data["sqlalchemy_uri"] = engine_spec.build_sqlalchemy_uri(
parameters,
encrypted_extra,
)
Expand Down Expand Up @@ -482,7 +482,6 @@ class Meta: # pylint: disable=too-few-public-methods


class DatabaseTestConnectionSchema(Schema, DatabaseParametersSchemaMixin):

rename_encrypted_extra = pre_load(rename_encrypted_extra)

database_name = fields.String(
Expand Down
2 changes: 1 addition & 1 deletion superset/datasets/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,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/db_engine_specs/presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -1272,10 +1272,10 @@ def handle_cursor(cls, cursor: "Cursor", query: Query, session: Session) -> None
def _extract_error_message(cls, ex: Exception) -> str:
if (
hasattr(ex, "orig")
and type(ex.orig).__name__ == "DatabaseError" # type: ignore
and isinstance(ex.orig[0], dict) # type: ignore
and type(ex.orig).__name__ == "DatabaseError"
and isinstance(ex.orig[0], dict)
):
error_dict = ex.orig[0] # type: ignore
error_dict = ex.orig[0]
return "{} at {}: {}".format(
error_dict.get("errorName"),
error_dict.get("errorLocation"),
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

# 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/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def to_dict(self) -> Dict[str, Any]:
if self.error_type:
rv["error_type"] = self.error_type
if self.exception is not None and hasattr(self.exception, "to_dict"):
rv = {**rv, **self.exception.to_dict()} # type: ignore
rv = {**rv, **self.exception.to_dict()}
return rv


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 @@ -564,7 +564,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 @@ -583,9 +583,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 @@ -594,9 +592,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 @@ -190,7 +190,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
4 changes: 2 additions & 2 deletions superset/models/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ def datasets_trimmed_for_slices(self) -> List[Dict[str, Any]]:

return result

@property # type: ignore
def params(self) -> str: # type: ignore
@property
def params(self) -> str:
return self.json_metadata

@params.setter
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 @@ -275,7 +275,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 @@ -280,7 +280,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 @@ -305,7 +305,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
14 changes: 7 additions & 7 deletions superset/sqllab/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SqlLabException(SupersetException):
failed_reason_msg: str
suggestion_help_msg: Optional[str]

def __init__( # pylint: disable=too-many-arguments
def __init__(
self,
sql_json_execution_context: SqlJsonExecutionContext,
error_type: Optional[SupersetErrorType] = None,
Expand All @@ -48,13 +48,13 @@ def __init__( # pylint: disable=too-many-arguments
if exception is not None:
if (
hasattr(exception, "error_type")
and exception.error_type is not None # type: ignore
and exception.error_type is not None
):
error_type = exception.error_type # type: ignore
error_type = exception.error_type
elif hasattr(exception, "error") and isinstance(
exception.error, SupersetError # type: ignore
exception.error, SupersetError
):
error_type = exception.error.error_type # type: ignore
error_type = exception.error.error_type
else:
error_type = SupersetErrorType.GENERIC_BACKEND_ERROR

Expand All @@ -79,9 +79,9 @@ def _get_reason(
return ": {}".format(reason_message)
if exception is not None:
if hasattr(exception, "get_message"):
return ": {}".format(exception.get_message()) # type: ignore
return ": {}".format(exception.get_message())
if hasattr(exception, "message"):
return ": {}".format(exception.message) # type: ignore
return ": {}".format(exception.message)
return ": {}".format(str(exception))
return ""

Expand Down
7 changes: 2 additions & 5 deletions superset/sqllab/query_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ class SqlQueryRenderImpl(SqlQueryRender):
def __init__(
self, sql_template_factory: Callable[..., BaseTemplateProcessor]
) -> None:

self._sql_template_processor_factory = sql_template_factory # type: ignore
self._sql_template_processor_factory = sql_template_factory

def render(self, execution_context: SqlJsonExecutionContext) -> str:
query_model = execution_context.query
Expand All @@ -76,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
3 changes: 1 addition & 2 deletions superset/sqllab/sql_json_executer.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class SqlJsonExecutorBase(SqlJsonExecutor, ABC):

def __init__(self, query_dao: QueryDAO, get_sql_results_task: GetSqlResultsTask):
self._query_dao = query_dao
self._get_sql_results_task = get_sql_results_task # type: ignore
self._get_sql_results_task = get_sql_results_task


class SynchronousSqlJsonExecutor(SqlJsonExecutorBase):
Expand Down Expand Up @@ -163,7 +163,6 @@ def execute(
rendered_query: str,
log_params: Optional[Dict[str, Any]],
) -> SqlJsonExecutionStatus:

query_id = execution_context.query.id
logger.info("Query %i: Running query on a Celery worker", query_id)
try:
Expand Down
8 changes: 6 additions & 2 deletions superset/tasks/async_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ def load_chart_data_into_cache(
raise ex
except Exception as ex:
# TODO: QueryContext should support SIP-40 style errors
error = ex.message if hasattr(ex, "message") else str(ex) # type: ignore # pylint: disable=no-member
error = (
ex.message if hasattr(ex, "message") else str(ex)
) # pylint: disable=no-member
errors = [{"message": error}]
async_query_manager.update_job(
job_metadata, async_query_manager.STATUS_ERROR, errors=errors
Expand Down Expand Up @@ -157,7 +159,9 @@ def load_explore_json_into_cache( # pylint: disable=too-many-locals
if isinstance(ex, SupersetVizException):
errors = ex.errors # pylint: disable=no-member
else:
error = ex.message if hasattr(ex, "message") else str(ex) # type: ignore # pylint: disable=no-member
error = (
ex.message if hasattr(ex, "message") else str(ex)
) # pylint: disable=no-member
errors = [error]

async_query_manager.update_job(
Expand Down
Loading

0 comments on commit b76aa57

Please sign in to comment.