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

Type error when calling methods of protocol superclass #7160

Closed
medihack opened this issue Jan 29, 2024 · 4 comments
Closed

Type error when calling methods of protocol superclass #7160

medihack opened this issue Jan 29, 2024 · 4 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@medihack
Copy link

medihack commented Jan 29, 2024

Describe the bug

In previous versions of pyright the below code didn't have any type errors.

class Base(Protocol):
    def get_data(self):
        ...

class MyMixin:
    def get(self: Base):
        self.get_data()
        super().get_data()

But at least since v1.1.348 (and also the current v1.1.348) the last line (super().get_data()) leads to the error (as mentioned in the discussions):

error: Method "get_data" cannot be called because it is abstract and unimplemented (reportAbstractUsage)

VS Code extension or command-line

Currently running Pyright v1.1.349 from the command line.

@medihack medihack added the bug Something isn't working label Jan 29, 2024
@erictraut
Copy link
Collaborator

Thanks for the bug report. When you initially mentioned this in the discussion, I assumed it was related to this issue, but that was an incorrect assumption.

This issue is related to a different change that was required for conformance with the typing spec.

Your use case is a little different than the case covered by the typing spec because you've explicitly declared the type of the self parameter to be a protocol. (Technically, the type of self should be Base & Mixin where & represents an intersection, but intersections are not yet supported by the type system.)

I'll need to think more about how to implement this check in a way that is both conformant with the typing standard and enables your use case.

@medihack
Copy link
Author

medihack commented Jan 30, 2024

Getting type hints correctly for mixins in Django is a real pain (and this is not just my use case). And yes, I came to the same conclusion that intersections are missing in the Python type system. Especially when the Mixin itself has attributes, here is a real-world example:

class ViewProtocol(Protocol):
    request: HttpRequest

    def dispatch(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
        ...

    def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
        ...

    def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
        ...


class LockedMixinProtocol(ViewProtocol, Protocol):
    settings_model: type[AppSettings]
    section_name: str


class LockedMixin:
    def dispatch(
        self: LockedMixinProtocol, request: HttpRequest, *args: Any, **kwargs: Any
    ) -> HttpResponse:
        settings = self.settings_model.get()

        if settings.locked and not (is_logged_in_user(request.user) and request.user.is_superuser):
            if request.method != "GET":
                raise SuspiciousOperation()

            return TemplateView.as_view(
                template_name="core/section_locked.html",
                extra_context={"section_name": self.section_name},
            )(request)

        return super().dispatch(request, *args, **kwargs)

I hope there will be a solution (I really hate those # type: ignore ;-)). Protocols are also the way mypy recommends to handle Mixins. But those examples do not have any super calls.

erictraut added a commit that referenced this issue Jan 31, 2024
… a protocol class. This pattern is used for annotating mix-ins. In this case, pyright should not generate an error if the protocol's method isn't implemented. This addresses #7160.
erictraut added a commit that referenced this issue Jan 31, 2024
… a protocol class. This pattern is used for annotating mix-ins. In this case, pyright should not generate an error if the protocol's method isn't implemented. This addresses #7160. (#7168)
@erictraut
Copy link
Collaborator

This will be addressed in the next release.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Jan 31, 2024
Domma143 added a commit to Domma143/pyright that referenced this issue Feb 2, 2024
commit 7c060cb
Author: Eric Traut <eric@traut.com>
Date:   Fri Feb 2 12:58:09 2024 -0800

    Another tweak to improve interruptions.

commit e5f13bb
Author: Eric Traut <eric@traut.com>
Date:   Fri Feb 2 00:56:00 2024 -0800

    Fixed a bug that results in a false positive error when a TypeVar bound to a union of literals is used in the specialization of a TypeAlias whose TypeVar is bound to a wider union of literals. This addresses microsoft#7184. (microsoft#7187)

commit 42d853f
Author: Eric Traut <eric@traut.com>
Date:   Wed Jan 31 20:50:15 2024 -0800

    Fixed a bug that results in a false negative when a `None` type is in… (microsoft#7176)

    * Fixed a bug that results in a false negative when a `None` type is included in an unpacked argument within a function call. This addresses microsoft#7175.

    * Improved logic for unpacked arguments that contain an "Unbound" type.

commit a81633d
Author: Eric Traut <eric@traut.com>
Date:   Wed Jan 31 16:59:53 2024 -0800

    Fixed a bug that resulted in an incorrect type evaluation when a TypeVar with a default (PEP 696) was used in an overload but was not solved. This addresses microsoft#7173. (microsoft#7174)

commit e01b0fe
Author: Rich Chiodo <rchiodo@users.noreply.github.com>
Date:   Wed Jan 31 16:38:37 2024 -0800

    Add test(s) that validate Pyright can talk over LSP (microsoft#7172)

    * Everything building, but not running

    * More tests passing

    * Fix test to open a file

    * Remove unused functions and consolidate others

    * Remove unused custom lsp messages

    * Add comments

commit 7585378
Author: Eric Traut <eric@traut.com>
Date:   Tue Jan 30 21:04:24 2024 -0800

    Changed behavior of `super()` method call when `self` is annotated as a protocol class. This pattern is used for annotating mix-ins. In this case, pyright should not generate an error if the protocol's method isn't implemented. This addresses microsoft#7160. (microsoft#7168)

commit 8af045b
Author: Erik De Bonte <erikd@microsoft.com>
Date:   Tue Jan 30 18:39:58 2024 -0800

    Grant write permissions as needed to workflows that use GITHUB_TOKEN (microsoft#7165)

commit 12e9dd9
Author: seairth <Seairth@users.noreply.github.com>
Date:   Mon Jan 29 12:48:49 2024 -0500

    Added multi-root workspaceFolder support in path variable expansion (microsoft#7138)

    Co-authored-by: Seairth Jacobs <sjacobs@wrsystems.com>

commit 419ae42
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 28 23:37:23 2024 -0800

    Published 1.1.349

commit 6457531
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 28 23:31:20 2024 -0800

    Fixed a bug that resulted in incorrect type narrowing for sequence patterns when the subject expression contains a tuple with an unbounded component. This addresses microsoft#7117. (microsoft#7156)

commit c8c8cea
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 28 21:14:11 2024 -0800

    Fixed a bug that resulted in incorrect type evaluation when calling a `tuple` constructor with bidirectional type inference and the value passed to the constructor is an `Iterable[Any]`. This addresses microsoft#7085. (microsoft#7155)

commit 7362545
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 28 18:07:14 2024 -0800

    Added additional performance enhancements specifically for large unions of literal types. This addresses microsoft#7143. (microsoft#7154)

commit 31f09f3
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 28 13:59:42 2024 -0800

    Added missing check for the use of `Annotated` or a type alias defined with `Annotated` as the second argument to an `isinstance` or `issubclass` call. This produces an exception at runtime. This addresses microsoft#7092.

commit 509bf69
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 28 13:49:04 2024 -0800

    Fixed bug that resulted in incorrect type evaluation when a TypeAliasType is used in a value expression. This addresses microsoft#7109.

commit 928d1cf
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 28 13:37:41 2024 -0800

    Fixed a bug that results in a false negative when the literal `typing.Any` is passed to a function that accepts a `type[T]`. `Any` is a special form and should not be compatible with `type`. This addresses microsoft#7082. (microsoft#7153)

commit 1d4b1cc
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 28 13:02:46 2024 -0800

    Fixed bug in tuple type compatibility logic that resulted in a false negative when dest type includes an upacked unbounded tuple plus additional entries. This addresses microsoft#7115. (microsoft#7152)

    Fixed bug in tuple type compatibility logic that resulted in a false positive when dest type is `tuple[Any, ...]`. This addresses microsoft#7129.
    Improved error messages for tuple type mismatches that involve tuples with indeterminate types.

commit 725f91f
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 28 10:43:07 2024 -0800

    Fixed a bug that results in a false positive when using a TypeVarTuple to capture the parameters of a generic callable that includes one or more default argument values. This addresses microsoft#7146. (microsoft#7150)

commit e18963f
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 28 10:23:40 2024 -0800

    Added support for enum member aliases defined within an enum class. This addresses microsoft#7142. (microsoft#7149)

commit c7d5fbd
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 28 10:07:56 2024 -0800

    Added special-case logic to handle `__name__` and a few other instanc… (microsoft#7148)

    * Added special-case logic to handle `__name__` and a few other instance variables defined in the `type` class so they are treated specially when accessed from a class instance. This addresses microsoft#7145.

    * Fixed style issue.

commit 9fb26eb
Author: Blake Naccarato <blake.naccarato@gmail.com>
Date:   Fri Jan 26 09:22:57 2024 -0800

    Clarify virtual environment language in `exclude` part (microsoft#7133)

commit ee4e30c
Author: Eric Traut <eric@traut.com>
Date:   Thu Jan 25 20:04:18 2024 -0800

    Added missing check for inappropriate use of `Final` in a value expression. This addresses microsoft#7094.

commit 9c0c056
Author: Eric Traut <eric@traut.com>
Date:   Thu Jan 25 19:25:46 2024 -0800

    Fixed regression that resulted in a false positive error when calling an abstract method on an abstract class that passes through the constraint solver (e.g. a generic decorator). This addresses microsoft#7105. (microsoft#7127)

commit df5d48c
Author: Eric Traut <eric@traut.com>
Date:   Thu Jan 25 01:26:05 2024 -0800

    Fixed a bug that resulted in an incorrect type evaluation for a union type used as a runtime expression. The type should be `UnionType`, not `type[UnionType]`. (microsoft#7121)

commit d94df3a
Author: Eric Traut <eric@traut.com>
Date:   Thu Jan 25 00:44:20 2024 -0800

    Changed handling of `tuple` with multiple unpacked embedded tuples. Type spec now clarifies this is OK as long as there are not multiple unbounded embedded tuples. This addresses microsoft#7114. (microsoft#7120)

commit dcc33ca
Author: Eric Traut <eric@traut.com>
Date:   Wed Jan 24 23:33:09 2024 -0800

    Changed the way pyright translates `tuple[()]` into a specialized `Sequence`. It used to translate it to `Sequence[Any]`, but the typing spec now clarifies that it should be `Sequence[Never]`. This addresses microsoft#7118. (microsoft#7119)

commit ebebb7f
Author: Eric Traut <eric@traut.com>
Date:   Wed Jan 24 16:43:03 2024 -0800

    Removed name consistency match for functional form of Enum. After a discussion in the typing community, it was decided that name consistency checks in some cases were unnecessary and inappropriate for type checkers.

commit cfb1de0
Author: Eric Traut <eric@traut.com>
Date:   Mon Jan 22 12:29:32 2024 -0800

    Fixed default for `reportImplicitStringConcatenation` in schema file.

commit 5aa1044
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 21 20:35:11 2024 -0800

    Added a few useful links to the docs.

commit 3ebcef2
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 21 20:28:15 2024 -0800

    Fixed minor doc bug.

commit 015e143
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 21 17:05:31 2024 -0800

    Removed tests from typeshed stubs. We don't use these, so they're just taking up unnecessary space.

commit a168f42
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 21 09:50:43 2024 -0800

    Published 1.1.348

commit 197ecd7
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 21 02:20:52 2024 -0800

    Added three new diagnostic rules: `reportArgumentType` covers argument type compatibility checks, `reportAssignmentType` covers type compatibility checks for assignments, and `reportReturnType` covers type compatibility checks for return and yield statements. This partially addresses microsoft#6973. (microsoft#7077)

commit 6ac1a7e
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 21 01:47:43 2024 -0800

    Added new diagnostic rule `reportCallIssue` that covers issues relating to call expressions and arguments. This partially addresses microsoft#6973. (microsoft#7076)

commit 6363745
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 21 01:35:19 2024 -0800

    Added two new diagnostic rules: `reportAttributeAccessIssue` is related to attribute accesses and `reportIndexIssue` is related to index operations and expressions. This partially addresses microsoft#6973. (microsoft#7075)

commit 8270551
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 21 01:07:49 2024 -0800

    Added two new diagnostic rules: `reportAbstractUsage` reports invalid use of abstract classes and methods and `reportOperatorIssue` covers diagnostics related to unary and binary operators. This partially addresses microsoft#6973. (microsoft#7074)

commit ec6052e
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 21 00:33:05 2024 -0800

    Added two new diagnostic rules: `reportInvalidTypeArguments` reports invalid type arg usage and `reportRedeclaration` reports attempts to redeclare the type of a symbol. This partially addresses microsoft#6973. (microsoft#7073)

commit 7a67f4f
Author: Eric Traut <eric@traut.com>
Date:   Sun Jan 21 00:04:55 2024 -0800

    Added two new diagnostic rules: `reportInconsistentOverload` reports inconsistencies between overload signatures and/or implementation and `reportNoOverloadImplementation` reports an overloaded function with a missing implementation. This partially addresses microsoft#6973. (microsoft#7072)

commit 91960fb
Author: Eric Traut <eric@traut.com>
Date:   Sat Jan 20 23:34:11 2024 -0800

    Added new diagnostic rule `reportPossiblyUnboundVariable`, which is split off from `reportUnboundVariable`. This addresses microsoft#6896. (microsoft#7071)

commit aa64fc5
Author: Eric Traut <eric@traut.com>
Date:   Sat Jan 20 22:21:42 2024 -0800

    Added two new diagnostic rules: `reportAssertTypeFailure` for type mismatches detected by `typing.assert_type` and `reportUnusedExcept` for situations where an except statement is determined to be unreachable. (microsoft#7070)

commit 04e0536
Author: Eric Traut <eric@traut.com>
Date:   Sat Jan 20 21:56:10 2024 -0800

    Added new diagnostic rule `reportInvalidTypeForm` that controls reporting of invalid type expression forms. This partly addresses microsoft#6973. (microsoft#7069)

commit 566f333
Author: Eric Traut <eric@traut.com>
Date:   Sat Jan 20 18:35:53 2024 -0800

    Fixed a bug that led to a false negative when an illegal form of tuple was used: `tuple[*tuple[str], ...]`. (microsoft#7066)

commit 3c36b30
Author: Eric Traut <eric@traut.com>
Date:   Sat Jan 20 17:32:49 2024 -0800

    Changed diagnostic rule for the case where `Callable` is missing a second type argument. It should use `reportMissingTypeArgument`.

commit 32f0685
Author: Eric Traut <eric@traut.com>
Date:   Sat Jan 20 17:32:16 2024 -0800

    Modernized a few diagnostic messages to refer to `tuple` (lowercase) rather than `Tuple` (uppercase).

commit 578ec79
Author: Eric Traut <eric@traut.com>
Date:   Sat Jan 20 14:55:25 2024 -0800

    Fixed a bug that resulted in a false positive error and incorrect type evaluation when an assignment expression (walrus operator) is used in a comprehension. This addresses microsoft#6992. (microsoft#7064)

commit 301ee7d
Author: Eric Traut <eric@traut.com>
Date:   Sat Jan 20 14:36:29 2024 -0800

    Disabled debug check that was accidentally enabled several builds ago.

commit d21168e
Author: Eric Traut <eric@traut.com>
Date:   Fri Jan 19 22:08:10 2024 -0800

    Updated typeshed stubs to the latest version.

commit af44054
Author: Eric Traut <eric@traut.com>
Date:   Fri Jan 19 21:55:50 2024 -0800

    Fixed a bug that masked an error (false negative) under certain circumstances when evaluating a lambda. This addresses microsoft#7012. (microsoft#7059)

commit 50f677c
Author: Eric Traut <eric@traut.com>
Date:   Fri Jan 19 20:34:01 2024 -0800

    Addressed a bug that led to a false positive (missing error) when a "bare" TypeVar is used as a base class in a class statement. This addresses microsoft#7023. (microsoft#7058)

commit 335255f
Author: Eric Traut <eric@traut.com>
Date:   Fri Jan 19 17:41:53 2024 -0800

    Added error reporting for the situation where a generic instance vari… (microsoft#7057)

    * Added error reporting for the situation where a generic instance variable is accessed through a class object. This addresses microsoft#7051.

    * Fixed style issue.

commit f10455e
Author: Eric Traut <eric@traut.com>
Date:   Fri Jan 19 16:18:26 2024 -0800

    Changed handling of `tuple[Any, ...]` so it is treated as though it's bidirectionally type compatible with all tuples regardless of length. This addresses microsoft#7053. (microsoft#7054)

commit 9ec6beb
Author: PylanceBot <99766470+PylanceBot@users.noreply.github.com>
Date:   Fri Jan 19 16:05:58 2024 -0800

    pull-pylance-with-pyright- (microsoft#7052)

    Co-authored-by: GitHub Actions <41898282+github-actions[bot]@users.noreply.github.com>
    Co-authored-by: rchiodo <rchiodo@microsoft.com>

commit 0fc5682
Author: Eric Traut <eric@traut.com>
Date:   Fri Jan 19 09:40:37 2024 -0800

    Improved handling of `Annotated` and other special forms when they are used in runtime value expressions rather than annotations. This addresses microsoft#7049. (microsoft#7050)

commit 01b1aa0
Author: Eric Traut <eric@traut.com>
Date:   Fri Jan 19 00:33:55 2024 -0800

    Added type enforcement for the `_value_` type in an Enum class. Also added enforcement for custom `__new__` and `__init__` method signatures. This addresses microsoft#7030 and microsoft#7029. (microsoft#7044)

commit 85e8de6
Author: Eric Traut <eric@traut.com>
Date:   Thu Jan 18 20:26:01 2024 -0800

    Added special-case logic for enum classes that are invoked as though they are being constructed. This addresses microsoft#7027. (microsoft#7042)

commit 3f3a86a
Author: Eric Traut <eric@traut.com>
Date:   Thu Jan 18 18:33:13 2024 -0800

    Improved handling of custom Enum classes. This addresses microsoft#7024. (microsoft#7041)

commit 4df6b6b
Author: Eric Traut <eric@traut.com>
Date:   Thu Jan 18 17:07:50 2024 -0800

    Fixed spec conformance issue with TypeVarTuple constraint solving. The spec indicates that if a TypeVarTuple is used multiple times in a callee's signature, the tuple must "match exactly". This addresses microsoft#6888. (microsoft#7040)

commit 922e746
Author: Eric Traut <eric@traut.com>
Date:   Thu Jan 18 12:14:47 2024 -0800

    Added check for name mismatch for enum classes defined using the functional syntax. This addresses microsoft#7025. (microsoft#7038)

commit 81e85d1
Author: Rich Chiodo <rchiodo@users.noreply.github.com>
Date:   Thu Jan 18 10:54:40 2024 -0800

    Fix combining paths with empty uri (microsoft#7037)

    * Fix combining paths with empty uri

    * Force empty to be case sensitive

    * Remove unneeded code
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.350, which I just published.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants