#🔒 How to type a type?

16 messages · Page 1 of 1 (latest)

dawn stirrup
#

I had closed #1367945514699718697 but the solution I was using is not working as expected.

I have

type RuleType = Figure | str | bool | int | float | list[RuleType]


class Rule:
    def __init__(self, rule_type: RuleType):
        ...

But when I use this:

Rule(
    rule_type=bool
)

I get a warning from my IDE: Expected type 'Figure | str | bool | int | float | list', got 'Type[bool]' instead

safe etherBOT
#

@dawn stirrup

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

dawn stirrup
#

@lost spoke @noble vault pinging because you were both helping me in my original thread

lost spoke
#

helloo

lost spoke
#

the class itself named bool has a type of type[bool]

dawn stirrup
#

ahh so I should do type RuleType = type[Figure] | ...?

lost spoke
#
class A:
    pass

x = A # type is 'type[A]'
x = A() # type is 'A'
lost spoke
#

OR you could just do type[RuleType]

#

or some other form

dawn stirrup
#

ohh thats smart

#

thank you!

#

!close

safe etherBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.