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

Accessing undeclared field does not raise a type error #51

Open
LuKuangChen opened this issue Oct 14, 2021 · 2 comments
Open

Accessing undeclared field does not raise a type error #51

LuKuangChen opened this issue Oct 14, 2021 · 2 comments
Labels
sp-correctness static python correctness staticpython static python issues

Comments

@LuKuangChen
Copy link

6862bbd
2021-10-13

What program did you run?

# lookup_self_field_neg.py
# This should fail.

class C:
    pass

def f(c: C):
    # reveal_type(c.x) # dynamic
    return c.x

What happened?

The program passes type checks.

What should have happened?

We expected a compile-time error complaining that "C has no attribute x". (This is what mypy does.)

Is this another case where Static Python falls back to dynamic (similar to #50)?

@carljm
Copy link
Contributor

carljm commented Oct 14, 2021

Yes, this is a case where we intentionally fall back to dynamic. The problem with strictly enforcing attributes is that we'd kind of like to be able to support cases where people are doing more dynamic things in building their class that our compiler doesn't necessarily understand (e.g. tacking on extra methods after the fact or via dynamic code in the class body, using class decorators that return the same type but maybe tack on extra stuff, maybe soon even some metaclasses); there's not much advantage to us in rejecting stuff like this (makes adoption harder) when we can just as easily fall back to dynamic and allow them to succeed or fail at runtime. Does it cause a problem for you?

I could potentially envision a stricter mode where we would enforce only using attributes that the compiler knows about; it's certainly not hard to implement. I think the ideal behavior here really depends somewhat on whether you are writing new code or trying to convert and gain whatever benefits you can in an existing dynamic codebase.

@LuKuangChen
Copy link
Author

Understandable. This is not a problem for us. We just wanted to know if this fall-back-to-dynamic behavior is intended.

@carljm carljm added sp-correctness static python correctness staticpython static python issues labels Jan 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sp-correctness static python correctness staticpython static python issues
Projects
None yet
Development

No branches or pull requests

2 participants