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

None is subtype of everything? #65

Closed
jasonkuhrt opened this issue May 30, 2018 · 6 comments
Closed

None is subtype of everything? #65

jasonkuhrt opened this issue May 30, 2018 · 6 comments
Labels

Comments

@jasonkuhrt
Copy link

Why does basic type check?

import random
import string


def gen(allowed_chars: string, length: int) -> str:
    ''.join([random.choice(allowed_chars) for _ in range(1, length)])


def basic(length: int) -> str:
    gen(string.ascii_letters + string.digits, length)
❯ pyre check
 ƛ Found 1 type error!
source/lib/random_string.py:6:4 Incompatible return type [7]: Expected `str` but got `None`.

I tried strict mode on the module to no effect (unsurprisingly since its about Any, not None).

@dark
Copy link
Contributor

dark commented May 30, 2018

Hi @jasonkuhrt , Pyre is warning you that you gen does not return any value. You need to add a return statement for that to work. That is also the case for the basic function.
basic does not throw any visible error here because string is not a type (for the record, this is the stub for the string module: https://github.com/python/typeshed/blob/master/stdlib/3/string.pyi)
You can see more low-level errors like this with pyre --debug check:

foo.py:68:4 Incompatible return type [7]: Expected str but got None.
foo.py:70:0 Undefined type [11]: Type string is not defined.

After correcting the file, no errors are generated for me:

import random
import string

def gen(allowed_chars: str, length: int) -> str:
    return ''.join([random.choice(allowed_chars) for _ in range(1, length)])

def basic(length: int) -> str:
    return gen(string.ascii_letters + string.digits, length)

Feel free to reopen this issue if you have further related questions.

@dark dark closed this as completed May 30, 2018
@dark dark added the question label May 30, 2018
@jasonkuhrt
Copy link
Author

basic does not throw any visible error here because string is not a type

I don't understand this. basic returns None but its type says that it will return str. What am I doing wrong exactly?

@dark
Copy link
Contributor

dark commented May 30, 2018

Let me rephrase - the error about a missing return in basic is being masked by the use of an unknown type string. In cases like this we are trying to be conservative and not display the error because the type might be legitimate and simply not being defined in a stub.

@jasonkuhrt
Copy link
Author

@dark I see, thanks. Could I fix this at the project level via local defs? The issue is that I'm using e.g. ascii_letters which has def:

ascii_letters = ...  # type: str

@dark
Copy link
Contributor

dark commented May 30, 2018

@jasonkuhrt Using the imported string module is totally fine -- and it works!
The problem was with the following incorrect type definition for the parameter of gen: allowed_chars: string. String is a module, not a type, so the correct definition would be allowed_chars: str. Please see the fixed code I posted in my first comment for all things I needed to change.

@jasonkuhrt
Copy link
Author

@dark ahhhhh sorry my eyes glazed over that, of course. Thanks.

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

No branches or pull requests

2 participants