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

Fix additional mypy errors and expand CI checks to more files #2541

Merged
merged 3 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion astroid/_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def parse(

def parse_function_type_comment(type_comment: str) -> FunctionType | None:
"""Given a correct type comment, obtain a FunctionType object."""
func_type = ast.parse(type_comment, "<type_comment>", "func_type") # type: ignore[attr-defined]
func_type = ast.parse(type_comment, "<type_comment>", "func_type")
return FunctionType(argtypes=func_type.argtypes, returns=func_type.returns)


Expand Down
3 changes: 2 additions & 1 deletion astroid/brain/brain_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

from __future__ import annotations

from astroid import arguments, inference_tip, nodes
from astroid import arguments, nodes
from astroid.context import InferenceContext
from astroid.exceptions import UseInferenceDefault
from astroid.inference_tip import inference_tip
from astroid.manager import AstroidManager


Expand Down
6 changes: 3 additions & 3 deletions astroid/brain/brain_boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

"""Astroid hooks for understanding ``boto3.ServiceRequest()``."""

from astroid import extract_node
from astroid.builder import extract_node
from astroid.manager import AstroidManager
from astroid.nodes.scoped_nodes import ClassDef

BOTO_SERVICE_FACTORY_QUALIFIED_NAME = "boto3.resources.base.ServiceResource"


def service_request_transform(node):
def service_request_transform(node: ClassDef) -> ClassDef:
"""Transform ServiceResource to look like dynamic classes."""
code = """
def __getattr__(self, attr):
Expand All @@ -22,7 +22,7 @@ def __getattr__(self, attr):
return node


def _looks_like_boto3_service_request(node) -> bool:
def _looks_like_boto3_service_request(node: ClassDef) -> bool:
return node.qname() == BOTO_SERVICE_FACTORY_QUALIFIED_NAME


Expand Down
9 changes: 5 additions & 4 deletions astroid/brain/brain_builtin_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from functools import partial
from typing import TYPE_CHECKING, Any, NoReturn, Union, cast

from astroid import arguments, helpers, inference_tip, nodes, objects, util
from astroid import arguments, helpers, nodes, objects, util
from astroid.builder import AstroidBuilder
from astroid.context import InferenceContext
from astroid.exceptions import (
Expand All @@ -21,6 +21,7 @@
MroError,
UseInferenceDefault,
)
from astroid.inference_tip import inference_tip
from astroid.manager import AstroidManager
from astroid.nodes import scoped_nodes
from astroid.typing import (
Expand Down Expand Up @@ -844,7 +845,7 @@ def _class_or_tuple_to_container(
return class_container


def infer_len(node, context: InferenceContext | None = None):
def infer_len(node, context: InferenceContext | None = None) -> nodes.Const:
"""Infer length calls.

:param nodes.Call node: len call to infer
Expand All @@ -867,7 +868,7 @@ def infer_len(node, context: InferenceContext | None = None):
raise UseInferenceDefault(str(exc)) from exc


def infer_str(node, context: InferenceContext | None = None):
def infer_str(node, context: InferenceContext | None = None) -> nodes.Const:
"""Infer str() calls.

:param nodes.Call node: str() call to infer
Expand Down Expand Up @@ -926,7 +927,7 @@ def infer_dict_fromkeys(node, context: InferenceContext | None = None):
will be inferred instead.
"""

def _build_dict_with_elements(elements):
def _build_dict_with_elements(elements: list) -> nodes.Dict:
new_node = nodes.Dict(
col_offset=node.col_offset,
lineno=node.lineno,
Expand Down
3 changes: 2 additions & 1 deletion astroid/brain/brain_crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt

from astroid import nodes
from astroid.brain.helpers import register_module_extender
from astroid.builder import parse
from astroid.manager import AstroidManager


def _re_transform():
def _re_transform() -> nodes.Module:
return parse(
"""
from collections import namedtuple
Expand Down
3 changes: 2 additions & 1 deletion astroid/brain/brain_ctypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
"""
import sys

from astroid import nodes
from astroid.brain.helpers import register_module_extender
from astroid.builder import parse
from astroid.manager import AstroidManager


def enrich_ctypes_redefined_types():
def enrich_ctypes_redefined_types() -> nodes.Module:
"""
For each ctypes redefined types, overload 'value' and '_type_' members
definition.
Expand Down
3 changes: 2 additions & 1 deletion astroid/brain/brain_curses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt

from astroid import nodes
from astroid.brain.helpers import register_module_extender
from astroid.builder import parse
from astroid.manager import AstroidManager


def _curses_transform():
def _curses_transform() -> nodes.Module:
return parse(
"""
A_ALTCHARSET = 1
Expand Down
3 changes: 2 additions & 1 deletion astroid/brain/brain_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt

from astroid import nodes
from astroid.brain.helpers import register_module_extender
from astroid.builder import AstroidBuilder
from astroid.const import PY312_PLUS
from astroid.manager import AstroidManager


def datetime_transform():
def datetime_transform() -> nodes.Module:
"""The datetime module was C-accelerated in Python 3.12, so use the
Python source."""
return AstroidBuilder(AstroidManager()).string_build("from _pydatetime import *")
Expand Down
3 changes: 2 additions & 1 deletion astroid/brain/brain_dateutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

import textwrap

from astroid import nodes
from astroid.brain.helpers import register_module_extender
from astroid.builder import AstroidBuilder
from astroid.manager import AstroidManager


def dateutil_transform():
def dateutil_transform() -> nodes.Module:
return AstroidBuilder(AstroidManager()).string_build(
textwrap.dedent(
"""
Expand Down
3 changes: 2 additions & 1 deletion astroid/brain/brain_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from functools import partial
from itertools import chain

from astroid import BoundMethod, arguments, extract_node, nodes, objects
from astroid import BoundMethod, arguments, nodes, objects
from astroid.builder import extract_node
from astroid.context import InferenceContext
from astroid.exceptions import InferenceError, UseInferenceDefault
from astroid.inference_tip import inference_tip
Expand Down
3 changes: 2 additions & 1 deletion astroid/brain/brain_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt

from astroid import nodes
from astroid.brain.helpers import register_module_extender
from astroid.builder import parse
from astroid.manager import AstroidManager


def _hashlib_transform():
def _hashlib_transform() -> nodes.Module:
init_signature = "value='', usedforsecurity=True"
digest_signature = "self"
shake_digest_signature = "self, length"
Expand Down
5 changes: 3 additions & 2 deletions astroid/brain/brain_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
"""Astroid brain hints for some of the `http` module."""
import textwrap

from astroid import nodes
from astroid.brain.helpers import register_module_extender
from astroid.builder import AstroidBuilder
from astroid.manager import AstroidManager


def _http_transform():
def _http_transform() -> nodes.Module:
code = textwrap.dedent(
"""
from enum import IntEnum
Expand Down Expand Up @@ -140,7 +141,7 @@ def description(self):
return AstroidBuilder(AstroidManager()).string_build(code)


def _http_client_transform():
def _http_client_transform() -> nodes.Module:
return AstroidBuilder(AstroidManager()).string_build(
textwrap.dedent(
"""
Expand Down
9 changes: 7 additions & 2 deletions astroid/brain/brain_hypothesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def a_strategy(draw):

a_strategy()
"""
from typing import TYPE_CHECKING

from astroid.manager import AstroidManager
from astroid.nodes.scoped_nodes import FunctionDef

Expand All @@ -27,7 +29,7 @@ def a_strategy(draw):
)


def is_decorated_with_st_composite(node) -> bool:
def is_decorated_with_st_composite(node: FunctionDef) -> bool:
"""Return whether a decorated node has @st.composite applied."""
if node.decorators and node.args.args and node.args.args[0].name == "draw":
for decorator_attribute in node.decorators.nodes:
Expand All @@ -36,11 +38,14 @@ def is_decorated_with_st_composite(node) -> bool:
return False


def remove_draw_parameter_from_composite_strategy(node):
def remove_draw_parameter_from_composite_strategy(node: FunctionDef) -> FunctionDef:
"""Given that the FunctionDef is decorated with @st.composite, remove the
first argument (`draw`) - it's always supplied by Hypothesis so we don't
need to emit the no-value-for-parameter lint.
"""
if TYPE_CHECKING:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it is better to actually do this assert always. mypy is showing us we have unsafe code, we should make it safe by asserting our assumption so we can see where they don't hold and handle it accordingly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense. I wasn't sure about potential performance implications, but we can optimize later if necessary.

The PR has been updated.

assert isinstance(node.args.args, list)

del node.args.args[0]
del node.args.annotations[0]
del node.args.type_comment_args[0]
Expand Down
3 changes: 2 additions & 1 deletion astroid/brain/brain_namedtuple_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from typing import Final

import astroid
from astroid import arguments, bases, inference_tip, nodes, util
from astroid import arguments, bases, nodes, util
from astroid.builder import AstroidBuilder, _extract_single_node, extract_node
from astroid.context import InferenceContext
from astroid.exceptions import (
Expand All @@ -22,6 +22,7 @@
InferenceError,
UseInferenceDefault,
)
from astroid.inference_tip import inference_tip
from astroid.manager import AstroidManager

ENUM_QNAME: Final[str] = "enum.Enum"
Expand Down
3 changes: 2 additions & 1 deletion astroid/brain/brain_numpy_core_fromnumeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt

"""Astroid hooks for numpy.core.fromnumeric module."""
from astroid import nodes
from astroid.brain.helpers import register_module_extender
from astroid.builder import parse
from astroid.manager import AstroidManager


def numpy_core_fromnumeric_transform():
def numpy_core_fromnumeric_transform() -> nodes.Module:
return parse(
"""
def sum(a, axis=None, dtype=None, out=None, keepdims=None, initial=None):
Expand Down
3 changes: 2 additions & 1 deletion astroid/brain/brain_numpy_core_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import functools

from astroid import nodes
from astroid.brain.brain_numpy_utils import (
attribute_looks_like_numpy_member,
infer_numpy_member,
Expand All @@ -18,7 +19,7 @@
from astroid.nodes.node_classes import Attribute, Name


def numpy_core_multiarray_transform():
def numpy_core_multiarray_transform() -> nodes.Module:
return parse(
"""
# different functions defined in multiarray.py
Expand Down
3 changes: 2 additions & 1 deletion astroid/brain/brain_numpy_core_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import functools

from astroid import nodes
from astroid.brain.brain_numpy_utils import (
attribute_looks_like_numpy_member,
infer_numpy_member,
Expand All @@ -17,7 +18,7 @@
from astroid.nodes.node_classes import Attribute


def numpy_core_numeric_transform():
def numpy_core_numeric_transform() -> nodes.Module:
return parse(
"""
# different functions defined in numeric.py
Expand Down
3 changes: 2 additions & 1 deletion astroid/brain/brain_numpy_core_numerictypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
# TODO(hippo91) : correct the methods signature.

"""Astroid hooks for numpy.core.numerictypes module."""
from astroid import nodes
from astroid.brain.brain_numpy_utils import numpy_supports_type_hints
from astroid.brain.helpers import register_module_extender
from astroid.builder import parse
from astroid.manager import AstroidManager


def numpy_core_numerictypes_transform():
def numpy_core_numerictypes_transform() -> nodes.Module:
# TODO: Uniformize the generic API with the ndarray one.
# According to numpy doc the generic object should expose
# the same API than ndarray. This has been done here partially
Expand Down
3 changes: 2 additions & 1 deletion astroid/brain/brain_numpy_core_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
# typecheck in `_emit_no_member` function)

"""Astroid hooks for numpy.core.umath module."""
from astroid import nodes
from astroid.brain.helpers import register_module_extender
from astroid.builder import parse
from astroid.manager import AstroidManager


def numpy_core_umath_transform():
def numpy_core_umath_transform() -> nodes.Module:
ufunc_optional_keyword_arguments = (
"""out=None, where=True, casting='same_kind', order='K', """
"""dtype=None, subok=True"""
Expand Down
3 changes: 2 additions & 1 deletion astroid/brain/brain_numpy_ma.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

"""Astroid hooks for numpy ma module."""

from astroid import nodes
from astroid.brain.helpers import register_module_extender
from astroid.builder import parse
from astroid.manager import AstroidManager


def numpy_ma_transform():
def numpy_ma_transform() -> nodes.Module:
"""
Infer the call of various numpy.ma functions.

Expand Down
3 changes: 2 additions & 1 deletion astroid/brain/brain_numpy_random_mtrand.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

# TODO(hippo91) : correct the functions return types
"""Astroid hooks for numpy.random.mtrand module."""
from astroid import nodes
from astroid.brain.helpers import register_module_extender
from astroid.builder import parse
from astroid.manager import AstroidManager


def numpy_random_mtrand_transform():
def numpy_random_mtrand_transform() -> nodes.Module:
return parse(
"""
def beta(a, b, size=None): return uninferable
Expand Down
3 changes: 2 additions & 1 deletion astroid/brain/brain_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

from collections.abc import Iterator

from astroid import bases, context, inference_tip, nodes
from astroid import bases, context, nodes
from astroid.builder import _extract_single_node
from astroid.const import PY313_PLUS
from astroid.exceptions import InferenceError, UseInferenceDefault
from astroid.inference_tip import inference_tip
from astroid.manager import AstroidManager

PATH_TEMPLATE = """
Expand Down
Loading