Why does Pylance not complain about missing members of a Protocol?

I am using Pylance with vscode and have set the type checking mode to ‘strict’

My understanding is that Pylance should flag Bar as not conforming to the Foo Protocol because of the commented foo method in Bar, but it does not.

I if uncomment the line it does correctly complain that ‘Method “foo” overrides class “Foo” in an incompatible manner’

Is there something I am missing?

from typing import Protocol
class Foo(Protocol):
  def foo(self, a: str): ...

class Bar(Foo): 
  # def foo(self, b: str): ...
  ...

# The above code generates no errors, however the following line does
a: Foo = Bar() # this generates a 'Cannot instantiate abstract class "Bar"' error in pylance

# but the file does run cleanly

The bottom line here is all I want to do is say that Bar implements Foo and have Pylance tell me if it does not, before I let someone try to create an instance of it.

I am subclassing Foo for 2 reasons.

  1. I want to document that Bar implements the Foo protocol
  2. I want to type check Bar where it is defined rather than when it is instantiated.

I am basing this on part of mypy documentation found here

As I said above, this works fine and flags incorrect method signatures right here in the file defining the Bar class.
The only thing it does not do is flag the missing method.
For that I can add something like the following:

bar: Foo = Bar()

This will highlight the missing method (because the Protocol makes Bar abstract), however it seems hacky to include this in the file defining Bar and also gets complicated if Bar has an __init__ function that takes several argument.

portion of mypy documentation linked above:

4

@larsks’s answer is partly incorrect: Inheriting a Protocol means to either implement or extend it, depending on whether Protocol itself is used as an explicit base class and whether the abstract members have default implementations.

As for implementing:

To explicitly declare that a certain class implements a given protocol, it can be used as a regular base class. In this case a class could use default implementations of protocol members.

— Protocols § Explicitly declaring implementation | Python typing spec

This does not apply to your example, however, since Foo.foo() does not have a default implementation.

As for extending:

The general philosophy is that protocols are mostly like regular ABCs, but a static type checker will handle them specially. Subclassing a protocol class would not turn the subclass into a protocol unless it also has typing.Protocol as an explicit base class. Without this base, the class is “downgraded” to a regular ABC that cannot be used with structural subtyping.

— Protocols § Merging and extending protocols | Python typing spec

In other words, neither your Bar has Protocol as one of its explicit base classes, nor does it implement the .foo() method, so it is considered an abstract class.

You can verify this by trying to instantiate it:

(playgrounds: Mypy, Pyright)

Bar()  # error: Cannot instantiate abstract class "Bar"

I found the solution I was looking for here.
Importing final from typing and decorating the class that I expect to implement the full protocol with @final does flag the missing method.

from typing import Protocol, final
class Foo(Protocol):
  def foo(self, a: str): ...

@final
class Bar(Foo):  #pylance error here 
  # def foo(self, b: str): ...
  ...

If I ever actually want to subclass Bar I can just ignore that type error.

class FooBar(Bar): # type: ignore - Bar is only marked final to ensure all protocol members are implemented
  pass

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật