GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Typing for metaclass examples · Issue #42 · fluentpython/example-code-2e

Typing for metaclass examples #42

New issue
Copy link
New issue
Copy link
Open
Open
Typing for metaclass examples#42
Copy link

Description

@jsrozner
jsrozner
opened on Apr 5, 2024
Issue body actions

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?

Activity

  1. ramalho commented on Apr 11, 2024

    @ramalho
    ramalho
    on Apr 11, 2024
    Member
    More actions

    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.

  2. ramalho commented on Apr 11, 2024

    @ramalho
    ramalho
    on Apr 11, 2024
    Member
    More actions

    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 !

    Reacted by Josh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.