basedmypy icon indicating copy to clipboard operation
basedmypy copied to clipboard

support `str` literals without `Literal`

Open DetachHead opened this issue 3 years ago • 5 comments

a: "asdf" | "fdsa" = "asdf"

https://github.com/KotlinIsland/basedmypy/pull/137#discussion_r927212154

DetachHead avatar Jul 23 '22 09:07 DetachHead

#1

KotlinIsland avatar Jul 23 '22 10:07 KotlinIsland

#137 is currently set to close #1 but it doesn't fully resolve it. should it be split into separate issues?

DetachHead avatar Jul 23 '22 10:07 DetachHead

yeah

KotlinIsland avatar Jul 23 '22 10:07 KotlinIsland

I wonder what the plan is for dealing with this:

>>> class A:
	@staticmethod
	def foo() -> A:
		return A()

	
Traceback (most recent call last):
  File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode
    exec(code, self.locals)
  File "<pyshell#4>", line 1, in <module>
  File "<pyshell#4>", line 3, in A
NameError: name 'A' is not defined

So, the solution is to put A in quotes:

>>> class A:
	@staticmethod
	def foo() -> "A":  # Does this return the literal "A"? or an instance of A?
		return A()

	
>>> A.foo()
<__main__.A object at 0x7f086e4e3490>

How do you tell apart the literal "A" from the A?

beauxq avatar Sep 21 '22 21:09 beauxq

We were thinking something like:

class A:
  a: ForwardRef["A"]

KotlinIsland avatar Sep 22 '22 03:09 KotlinIsland