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

Typing for metaclass examples #42

Open
jsrozner opened this issue Apr 5, 2024 · 2 comments
Open

Typing for metaclass examples #42

jsrozner opened this issue Apr 5, 2024 · 2 comments

Comments

@jsrozner
Copy link

jsrozner commented Apr 5, 2024

Consider the example in 24-class-metaprog/factories.py (corresponds to example 24-2, page 912 in my copy)

Dog = record_factory('Dog', 'name weight owner') 
rex = Dog('Rex', 30, 'Bob')

We do not get static type checking on, e.g., rex.name

Is there an easy fix, or is this because types in metaclasses / metaprogramming are hard?

@ramalho
Copy link
Member

ramalho commented Apr 11, 2024

We do not get static type checking because all effects of metaprogramming happen at runtime, and a static type checker does not run your program. It can only analyze the static source code, and it is not smart enough to understand what record_factory does, or that the string `'name weight owner' is actually naming attributes of a class that will be built at runtime.

@ramalho
Copy link
Member

ramalho commented Apr 11, 2024

This is an interesting question, so I will leave the issue open until I work on chapter 24 for the third edition, hopefully in 2025.

Thanks, @jsrozner !

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

No branches or pull requests

2 participants