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

Infer magic methods #1

Closed
orsinium opened this issue Jun 29, 2021 · 5 comments · Fixed by #7
Closed

Infer magic methods #1

orsinium opened this issue Jun 29, 2021 · 5 comments · Fixed by #7

Comments

@orsinium
Copy link
Contributor

Thank you for the project. I tried it on the project, and it works. At least, doesn't break anything :)

The return type for some magic methods can be also auto-annotated. A few examples:

  1. __str__ and __repr__ return str
  2. __init__ returns None
  3. iter returns typing.Iterable
@JelleZijlstra
Copy link
Owner

Thanks, that's great to hear! I'm going to do some more work on in this in the next couple of weeks. I agree with __str__ and __repr__, but I don't think we can do __iter__, because you really want Iterable[SomeType] and autotyper is not nearly smart enough to know what that type should be.

Other methods we could infer:

  • __exit__ arguments should be Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]
  • __len__ should return int
  • __del__ should return None
  • __bool__ should return bool
  • __bytes__ should return bytes
  • __format__ should return str
    Other methods in https://docs.python.org/3/reference/datamodel.html don't look safe to infer.

@orsinium
Copy link
Contributor Author

but I don't think we can do iter, because you really want Iterable[SomeType]

This is an interesting topic to discuss, BTW. I think autotyper can partially infer some types because it is better than nothing. In this case, typing.Iterator (or more explicitly, typing.Iterator[typing.Any]) will already allow mypy to find some bugs. The same applies to generic types like typing.List (or just list).

@JelleZijlstra
Copy link
Owner

That's a fair point. It will also introduce new mypy errors if you have --disallow-any-generics on, but on a codebase that you use autotyper on you might not want to worry about strictness options yet. :)

@JelleZijlstra
Copy link
Owner

Just merged support for most of this, except for __exit__ which I split out into #8.

Let me know if you have more ideas for improvements!

@orsinium
Copy link
Contributor Author

Great! Thank you. I'll give it a try :)

Beautiful project, BTW, I always wanted something like this. I've added it into awesome-python-typing.

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

Successfully merging a pull request may close this issue.

2 participants