#๐Ÿ”’ Dynamic Classes: MYPY saying `Variable is not valid as a type`, any suggested fixes?

17 messages ยท Page 1 of 1 (latest)

cobalt plover
#

Firstly, you can peep the example code here: https://paste.pythondiscord.com/EZ6Q

My big question here is why does class A seem fine to MyPy, but class B gives an error? Is there some way I can make the function B work? It's for instances when the bar_cls arguments fulfill my "modifications" to the class, and I don't need to define a class using the class keyword as I won't need to modify it's inner functions or anything. If the answer is that I just need to construct all of these subclasses like A then I guess that works too.

Using python 3.12

opal berryBOT
#

@cobalt plover

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.

sacred vapor
#

what line?

#

I suspect at least 14, 19, 25, 26

#

37, 37, 38, 38, 46, 46

#

47

cobalt plover
#

sorry, on line 62 where I have put the comment with the error

sacred vapor
#

oh, I thought those with lowercase t in type would have cause mypy complain

cobalt plover
#

I think mypy is fine with type instead of Type now. Using 3.12 too which might have been relevant

#

the error I commented in the code is the only mypy error that shows up

coral ingot
cobalt plover
#

im guessing there is something different enough about creating a class with type vs using the class keyword that it doesn't like for some reason. Even though to me, it should essentially be the same thing

coral ingot
#

If you ask in #type-hinting someone might know the reason behind why it was chosen to be like that

cobalt plover
#

oh right I forgot there was a type specific chat, lemme ping this in there

opal berryBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.