Skip to content

Pyrefly infers Never as the return type of functions that ends with a falsy condition #2546

@QEDady

Description

@QEDady

Describe the Bug

Pyrefly infers Never in the following return type:

def foo(): # return type inferred as Never
    print(42)
    if False:
        print(1)

I think the above is a bug. Pyright and ty would complain if we annotate the return type as Never.

However if you switch the order, pyrefly correctly infers None:

def foo(): # return type inferred as None
    if False:
        print(1)
    print(42)

We are adapting pyrefly to a large codebase and got a bunch of bad-override errors because of the above, specifically:

class A():
  def foo():
    print(42)
    if False:
         print(1)

class B(A):
  def foo(): # bad-override
    print(3)

In the above, we get

Class member `B.foo` overrides parent class `A` in an inconsistent manner [[bad-override](https://pyrefly.org/en/docs/error-kinds/#bad-override)]
  `B.foo` has type `() -> None`, which is not assignable to `() -> Never`, the type of `A.foo`
  Signature mismatch:
  expected: def foo() -> Never: ...
                          ^^^^ return type
  found:    def foo() -> None: ...
                          ^^^ return type

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIAxAAQDqMUAxrgLYzUAuunAFuwAqkATjDBRS1AMoYcBADS8IcakuqpqxVAHMYAHXQ0A7nxHVSuAK7VGGaoaEQOMBXB6pTM6rHfo1eCxycpMQQ6FoqvoIcPLjohPqUCdQAEjAeHtaWxLDUYDCoHBYiyrhgispoWHj4iEkAtNQA4jCBIqhQdRwQbEHE7Ix8jADWoeF5MDiow-XUAKpw7CIAbvlQAPocwTAAFACUnNyhcH2MgaF5QiKYvfAzMPh9DmzogYaOPLzsAJIAIgCiuXyhWKvAKdnYcAsxGIuCEgW2MEIWkIagCuGYLGyLWc1C0uE6%2BMwolCjggsQUMRWQgULUYuxmtAcTjUWGoQgskVI0VimSJ1EwEBEp3EETMliE1GwQlwhgWQn0RLKYFwuD2iGoNBEwN8mz6EQuVzUygAcjAqfpqJaNA4XtsACwAJnpvitEDKADF2gtai6rVbiDaONsAIzOhWiXIqtUatktIo6rb6tKG1Am2J6X0qD1emA%2Bv1%2BgOhIOhi3%2BwP2p36KvoRhQVPKACCatLisjqt2ebLRYrzr9buonqg3tL%2BcthdtJfQ%2Blr9eoACFtg2Oy2I8r252x%2BWAMy7EByEBkERiUiELgsKAUGiCI8iyqyfCZdCQLRFApkuJJSQwdg8DgcYhwIgAD0QGHqI4iELCWhATA6BAZg6JwEBzBPhAL5CG%2BsRAZGEqoEsqDQKg2A5Chz6vl0PK4MQFHoHA8ToGQ3LoHUVJwO%2B1AALzULoIBboQwYOjx%2BggAAvvuUxdCs7rQDAFB3tUJDkCJQA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

Labels

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions