Skip to content

Commit b954348

Browse files
Overhaul treatment of function definitions (#372)
1 parent 2174d28 commit b954348

14 files changed

+736
-640
lines changed

docs/changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
- Overhaul treatment of function definitions (#372)
6+
- Support positional-only arguments
7+
- Infer more precise types for lambda functions
8+
- Infer more precise types for nested functions
9+
- Refactor related code
510
- Add check for incompatible overrides in child classes
611
(#371)
712
- Add `pyanalyze.extensions.NoReturnGuard` (#370)

pyanalyze/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from . import extensions
2020
from . import find_unused
2121
from .find_unused import used as used
22+
from . import functions
2223
from . import implementation
2324
from . import method_return_type
2425
from . import node_visitor
@@ -50,3 +51,4 @@
5051
used(suggested_type)
5152
used(options)
5253
used(shared_options)
54+
used(functions)

pyanalyze/asynq_checker.py

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,20 @@
88
import asynq
99
import contextlib
1010
from dataclasses import dataclass, field
11-
import enum
1211
import qcore
1312
import inspect
1413
import types
15-
from typing import Sequence, Any, Callable, List, Optional, Iterable
16-
17-
from pyanalyze.options import Options, PyObjectSequenceOption, StringSequenceOption
14+
from typing import Iterator, Sequence, Any, Callable, Optional
1815

1916
from .config import Config
2017
from .error_code import ErrorCode
18+
from .functions import AsyncFunctionKind
19+
from .options import Options, PyObjectSequenceOption, StringSequenceOption
2120
from .safe import safe_getattr, safe_hasattr
2221
from .stacked_scopes import Composite
2322
from .value import AnnotatedValue, Value, KnownValue, TypedValue, UnboundMethodValue
2423

2524

26-
class AsyncFunctionKind(enum.Enum):
27-
non_async = 0
28-
normal = 1
29-
async_proxy = 2
30-
pure = 3
31-
32-
33-
@dataclass(frozen=True)
34-
class FunctionInfo:
35-
async_kind: AsyncFunctionKind
36-
is_classmethod: bool # has @classmethod
37-
is_staticmethod: bool # has @staticmethod
38-
is_decorated_coroutine: bool # has @asyncio.coroutine
39-
is_overload: bool # typing.overload or pyanalyze.extensions.overload
40-
# a list of pairs of (decorator function, applied decorator function). These are different
41-
# for decorators that take arguments, like @asynq(): the first element will be the asynq
42-
# function and the second will be the result of calling asynq().
43-
decorators: List[Any]
44-
45-
4625
class ClassesCheckedForAsynq(PyObjectSequenceOption[type]):
4726
"""Normally, asynq calls to asynq functions are only enforced in functions that are already
4827
asynq. In subclasses of classes listed here, all asynq functions must be called asynq."""
@@ -98,7 +77,7 @@ def set_func_name(
9877
name: str,
9978
async_kind: AsyncFunctionKind = AsyncFunctionKind.non_async,
10079
is_classmethod: bool = False,
101-
) -> Iterable[None]:
80+
) -> Iterator[None]:
10281
"""Sets the current function name for async data collection."""
10382
# Override current_func_name only if this is the outermost function, so that data access
10483
# within nested functions is attributed to the outer function. However, for async inner

0 commit comments

Comments
 (0)