Skip to content

how to get 'module' of NewType? #757

Closed

Description

@devdoomari3

is there a way to get module of a NewType?

example:

# -------- packageA/packageB/some_file.py file  --------

from typing import NewType, AnyStr

ValidatedEmail = NewType('ValidatedEmail', AnyStr)
...

    Extractor

# --------  extract_newtype.py  --------
from packageA.packageB.some_file import (
    ValidatedEmail
)

# how to access module (or module name) of SomeTypeVarA ?
ValidatedEmail.__module__  # points to 'typing'

# what I'm hoping to get:
get_module_from_typevar(ValidatedEmail) == 'packageA.packageB.some_file'

Activity

  1. gvanrossum commented on Sep 26, 2020

    @gvanrossum
    Member

    There is no way, and this is not a desirable use case. (Also, you're using the term "typevar" wrong. :-)

  2. devdoomari3 commented on Sep 27, 2020

    @devdoomari3
    Author

    sorry for being sloppy ( the 'typevars' in the above example are the result of copy-pasta...)

    my actual use case for module is for "generating Typescript-interface code with python project structure nearly intact"
    (so a ValidatedEmail inside py_proj/types/user.py will result in a nominal-type in generated_ts_proj/types/user.ts file)


    Anyway, for there's no way, do you mean:

    1. it can't be backported ( can't do pip install -U typing, so this will require newer python version (py3.11+?)
    2. current typing project isn't designed for this (impossible / may take big overhaul)

    in some related discussion from other repo, there's mention of "we can probably backport..." --- is this even possible?
    (maybe they're thinking of monkey-patching the typing module before everything else runs?)

    I'm not actually a python-person, so I'm not very familiar with the python-internals 😭

  3. gvanrossum commented on Sep 27, 2020

    @gvanrossum
    Member
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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions