Shortest way to get a TypeError

-4

1

It's simple, simply write the shortest code to raise a TypeError.

In Python you can do:

raise TypeError

However it should be shorter with a code that gives a TypeError without raising it.

U10-Forward

Posted 2019-07-31T02:24:25.893

Reputation: 347

1this isn't really defined for other languages. You've also immediately answered the shortest possible way for Python... – Jo King – 2019-07-31T02:29:43.530

@JoKing I answer one out of 3 or more, feel free to post the other 3 byte ones – U10-Forward – 2019-07-31T02:30:39.763

1This shouldn't be closed, but it's only well-defined for one language (Python) – MilkyWay90 – 2019-07-31T02:31:54.703

I now made this a python golf – U10-Forward – 2019-07-31T02:32:47.863

6I'm retracting my close vote, since this is pretty clear. But I'm downvoting this because it is an extremely simple challenge with an obvious optimal answer with no possibility of improvement. – Jo King – 2019-07-31T02:37:56.007

2

Oh right, you might want to post your challenges to the Sandbox before you post this here.

– None – 2019-07-31T03:27:10.713

maybe add the tips tag since it's language-specific. – Giuseppe – 2019-07-31T13:19:11.513

Answers

13

for chr1 in range(32, 128):
  for chr2 in range(32, 128):
    for chr3 in range(32, 128):
      code = chr(chr1) + chr(chr2) + chr(chr3)
      try:
        output = exec(code, {})
      except TypeError:
        print(code)
      except:
        pass

Try it online!

A naive search may result that you cannot trigger a TypeError within 2 characters. You may get a TypeError with 3 characters. All solutions including:

1. Math operator, non-numeric types

  • Math operator including +, -, ~
  • Non-numeric types including "", (), [], id, {}
+[]
-""
~id

2. Invoke number as function

0()

3. Matrix multiplication between numbers

@ is __matmul__ in python, read more here

0@0

4. Binary operator with float / complex number

~0.
~.0
~0j

5. Iterate over number

Thanks to @Dennis to point out this.

You may iterate some variable by star operator. And () for a tuple may be omitted.

*0,

tsh

Posted 2019-07-31T02:24:25.893

Reputation: 13 072

2

Python 3, 3 bytes

+''

Try it online!

U10-Forward

Posted 2019-07-31T02:24:25.893

Reputation: 347

1Uh oh. I can not make my program less than 3 bytes. Anyway, nice trick! – None – 2019-07-31T02:31:05.020

@A__ Yeah, anyway you can post the other 3 byte ones – U10-Forward – 2019-07-31T02:31:49.820

2

Python, 3 bytes

This is self-explanatory. The unary - operator does not take a string argument. This was covered in @tsh's solution above.

-""

Try it online!

Another one:

~id

Try it online!

user85052

Posted 2019-07-31T02:24:25.893

Reputation:

Haha yeah nice idea i still have more than 2 different styles in mind tho – U10-Forward – 2019-07-31T02:35:28.247

yeah, double quotes and that's about it – Jo King – 2019-07-31T02:36:01.170

There is also -id and +id

– Jo King – 2019-07-31T02:38:51.307

2

JavaScript (V8), 4 bytes

1.()

Try it online!

[Output]

.code.tio:1: TypeError: 1 is not a function 1.() ^ TypeError: 1 is not a function at .code.tio:1:3

Rajan Kumar

Posted 2019-07-31T02:24:25.893

Reputation: 71

2

Japt, 1 byte

Throws TypeError: U.í is not a function

í

Test it (Errors are displayed below the output field)


Or, a bit less trivial:

Japt, 2 bytes

Throws TypeError: (U++) is not a function

°(

Test it

Shaggy

Posted 2019-07-31T02:24:25.893

Reputation: 24 623