8000 Gradually building typed dict · Issue #2494 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Gradually building typed dict #2494

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

Closed
JukkaL opened this issue Nov 24, 2016 · 1 comment
Closed

Gradually building typed dict #2494

JukkaL opened this issue Nov 24, 2016 · 1 comment

Comments

@JukkaL
Copy link
Collaborator
JukkaL commented Nov 24, 2016

We may want to support code like this, since this style of creating dictionaries seems pretty common:

X = TypedDict('X', {'n': int, 'status': str})

def f() -> X
    x = {}  # type: X
    x['n'] = 3
    if cond():
        x['status'] = 'yes'
    else:
        x['status'] = 'no'
    return x

We could perhaps use partial types and the conditional type binder for this. The type of x would be partial until all items have been set.

This code would be rejected, since x is still partial when we read it:

def f() -> X:
    x = {}  # type: X
    x['n'] = 3
    if cond():
        x['status'] = 'yes'
    return x  # error: "status" of "x" may be undefined
@JukkaL
Copy link
Collaborator Author
JukkaL commented Jan 28, 2020

This seems too complicated to implement relative to the potential benefit.

@JukkaL JukkaL closed this as completed Jan 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant
0