-
-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement _StrPromise attribute hook.
This implements an attribute hook that provides type information for methods that are available on `builtins.str` for `_StrPromise` except the supported operators. This allows us to avoid copying stubs from the builtins for all supported methods on `str`. Signed-off-by: Zixuan James Li <[email protected]>
- Loading branch information
Showing
5 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from mypy.errorcodes import ATTR_DEFINED | ||
from mypy.nodes import MemberExpr | ||
from mypy.plugin import AttributeContext | ||
from mypy.types import AnyType, CallableType, Instance | ||
from mypy.types import Type as MypyType | ||
from mypy.types import TypeOfAny | ||
|
||
from mypy_django_plugin.lib import helpers | ||
|
||
|
||
def resolve_str_promise_attribute(ctx: AttributeContext) -> MypyType: | ||
assert isinstance(ctx.type, Instance) | ||
assert isinstance(ctx.context, MemberExpr) | ||
|
||
str_info = helpers.lookup_fully_qualified_typeinfo(helpers.get_typechecker_api(ctx), f"builtins.str") | ||
assert str_info is not None | ||
method = str_info.get(ctx.context.name) | ||
|
||
if method is None or method.type is None: | ||
ctx.api.fail( | ||
f'"{ctx.type.type.fullname}" has no attribute "{ctx.context.name}"', ctx.context, code=ATTR_DEFINED | ||
) | ||
return AnyType(TypeOfAny.from_error) | ||
|
||
assert isinstance(method.type, CallableType) | ||
# The proxied str methods are only meant to be used as instance methods. | ||
# We need to drop the first `self` argument in them. | ||
assert method.type.arg_names[0] == "self" | ||
return method.type.copy_modified( | ||
arg_kinds=method.type.arg_kinds[1:], arg_names=method.type.arg_names[1:], arg_types=method.type.arg_types[1:] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters