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

0.990 reports error for function with body that is just ellipsis #14048

Closed
haxtibal opened this issue Nov 9, 2022 · 6 comments · Fixed by #14065
Closed

0.990 reports error for function with body that is just ellipsis #14048

haxtibal opened this issue Nov 9, 2022 · 6 comments · Fixed by #14065
Labels
bug mypy got something wrong documentation

Comments

@haxtibal
Copy link

haxtibal commented Nov 9, 2022

mypy 0.990 (python 3.8) reports an error for the following source code

def foo() -> int:
    ...
$ mypy foo.py 
/tmp/foo.py:1: error: Missing return statement  [empty-body]
Found 1 error in 1 file (checked 1 source file)

mypy 0.981 would let the same code pass. Was that an intended change in between, or a regression?

From mypy CLI documentation

By default, mypy will generate errors when a function is missing return statements in some execution paths. The only exceptions are when:

  • The function has a None or Any return type
  • The function has an empty body or a body that is just ellipsis (...). Empty functions are often used for abstract methods.

I'd expect Success for the given example, because the 2nd exception ("body that is just ellipsis") applies.

@haxtibal haxtibal added the bug mypy got something wrong label Nov 9, 2022
@haxtibal haxtibal changed the title 0.990 reports 0.990 reports error for function with body that is just ellipsis Nov 9, 2022
@JukkaL
Copy link
Collaborator

JukkaL commented Nov 9, 2022

This was an intentional change in 0.990 and the special casing for ... bodies was mostly removed (#13729). The documentation seems to be now out of date.

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 9, 2022

cc @ilevkivskyi

@ilevkivskyi
Copy link
Member

Yep, I will update the docs. Now you need --allow-empty-bodies if you want old behavior (or disable empty-body error code in your mypy config)

@haxtibal
Copy link
Author

haxtibal commented Nov 9, 2022

Thank's for clarifying.

Now you need --allow-empty-bodies if you want old behavior

I'm unsure if I want the old behavior🤔 Actually I was ever in doubt if the way I used such ... stubs was good practice. If you have a minute left, would you consider this pseudo code:

class FooStub:
    # Serves as means to check calls to use_generated_foo() with mypy

    def bar() -> int:
        ...

class GeneratedFoo(RuntimeAttrGen, FooStub):
    # Attributes made available at runtime, according to external spec.
    # I control the spec and therefore know what methods and attributes will be available,    
    # but can't hardcode implementations here.
    pass


def use_generated_foo(x: FooStub)
    x.bar()

Would you recommend another approach for type checking such runtime-determined types? E.g., I'd rather avoid deriving from FooStub in favor of structural subtyping. But then I don't know how to associate GeneratedFoo with attributes and methods as in FooStub.

@ilevkivskyi
Copy link
Member

IIUC the situation, my personal preference would be to replace ... with raise AttributeNotGeneratedError. This way if empty stub is used accidentally, you will know immediately, instead of discovering random None somewhere down the call stack.

@ilevkivskyi
Copy link
Member

To clarify, mypy will also not complain about this.

@hauntsaninja hauntsaninja added pending Issues that may be closed and removed pending Issues that may be closed labels Nov 10, 2022
ilevkivskyi pushed a commit that referenced this issue Nov 11, 2022
Fixes #14048

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
hauntsaninja added a commit that referenced this issue Nov 11, 2022
Fixes #14048

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants