Skip to content

min/max are missing optional default keyword only argument #77

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No 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? No Sign in to your account

Closed
rmcgibbo opened this issue Feb 3, 2016 · 7 comments
Closed

min/max are missing optional default keyword only argument #77

rmcgibbo opened this issue Feb 3, 2016 · 7 comments

Comments

@rmcgibbo
Copy link
Contributor

rmcgibbo commented Feb 3, 2016

In py3, the min and max argument take an optional keyword only argument, default, but this isn't in the type signature stub.

https://github.com/python/typeshed/blob/master/stdlib/3/builtins.pyi#L660-L663

@gvanrossum
Copy link
Member

Aha! The fix for this was waiting for a change in mypy. That change has now landed (python/mypy#1150) so we can now accept a PR to add this.

@rmcgibbo
Copy link
Contributor Author

rmcgibbo commented Feb 3, 2016

I don't really know the @overload syntax. Should I just add another kwarg to https://github.com/python/typeshed/blob/master/stdlib/3/builtins.pyi#L663?

@gvanrossum
Copy link
Member

gvanrossum commented Feb 3, 2016 via email

@rmcgibbo
Copy link
Contributor Author

rmcgibbo commented Feb 3, 2016

I raised this issue about the default: _T=None arg, not key:. I'll try it out each way.

@gvanrossum
Copy link
Member

gvanrossum commented Feb 3, 2016 via email

@EdwardEisenhauer
Copy link

I experienced mix/max issues with the following code:

from typing import Optional


class MyObject:
    def __init__(self, attribute: int) -> None:
        self.attribute = attribute


def find_biggest_attribute(objects: list[MyObject]) -> Optional[MyObject]:
    return max(objects, default=None, key=lambda object: object.attribute)


print(find_biggest_attribute([MyObject(42), MyObject(41)]))

print(find_biggest_attribute([]))

Running mypy raises:

mypy_bug.py:10: error: Item "None" of "Optional[MyObject]" has no attribute "attribute"  [union-attr]
        return max(objects, default=None, key=lambda object: object.attribute)
                                                             ^~~~~~~~~~~~~~~~
Found 1 error in 1 file (checked 1 source file)

As per Python built-in max() documentation:

The default argument specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided, a ValueError is raised.

@JelleZijlstra
Copy link
Member

Please do not post on unrelated ancient issues. What you experience is a problem with mypy, not typeshed. It's probably the same as python/mypy#14664.

No Sign up for free to join this conversation on GitHub. Already have an account? No Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants