# Unexpected Type Error Decorator Exercise

**URL:** <https://forum.freecodecamp.org/t/unexpected-type-error-decorator-exercise/490838>\
**Category:** Curriculum Help\
**Tags:** python\
**Created:** [January 2, 2022, 6:26am UTC](https://forum.freecodecamp.org/t/unexpected-type-error-decorator-exercise/490838 "2022-01-02T06:26:49Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jagmeet](https://avatars.discourse-cdn.com/v4/letter/j/bc79bd/32.png) [@Jagmeet](https://forum.freecodecamp.org/u/Jagmeet)\
**Post date:** [January 2, 2022, 6:26am UTC](https://forum.freecodecamp.org/t/unexpected-type-error-decorator-exercise/490838/1 "2022-01-02T06:26:49Z")

</div>

**The Exercise**

Create a decorator `expected_type()` that checks if what the decorated function returns is of expected type. An `UnexpectedTypeException` should be raised if the type returned by the decorated function doesn’t match the ones expected.

Requirements:

- `expected_type()` should accept a tuple of many types that may be valid
- `UnexpectedTypeException` should be raised if decorated function returns object of type that wasn’t defined in `expected_type()` 's arguments (you have to implement that class)
- `return_something` will be decorated with `expected_type` in the tests and will look exactly like in the example below

```auto

#the decorator creation

def expected_type(*msg):

    def wrapper(return_something):

        for items in msg:

            x = return_something()

            if items == type(x):

                print("hello function is of the type expected")

               

            else:

                print("Type Unexpected Error")

                break

                   

    return wrapper

   

@expected_type('str','dict','set','list')

def return_something():

    return ('hello everyone')

# calling the function

print(return_something)

```

The Error l am getting is:

Type Unexpected Error  
None

I am expecting the code to print hello function is of the type expected but it is not working, how can l correct it?

Thanks

---

<div class="post-metadata">

**Author:** ![sanity](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sanity/32/378897_2.png) [@sanity](https://forum.freecodecamp.org/u/sanity)\
**Post date:** [January 2, 2022, 8:09am UTC](https://forum.freecodecamp.org/t/unexpected-type-error-decorator-exercise/490838/2 "2022-01-02T08:09:51Z")

</div>

Try to follow the `wrapper` function logic step-by-step. What each variable is, what happens with it in function, what is printed and when.

---

<div class="post-metadata">

**Author:** ![Jagmeet](https://avatars.discourse-cdn.com/v4/letter/j/bc79bd/32.png) [@Jagmeet](https://forum.freecodecamp.org/u/Jagmeet)\
**Post date:** [January 2, 2022, 11:55am UTC](https://forum.freecodecamp.org/t/unexpected-type-error-decorator-exercise/490838/3 "2022-01-02T11:55:37Z")

</div>

ok l have started to get what l want now, why am l getting None at the end?, my return\_something function has a return statement

```auto

#the decorator creation

def expected_type(*msg):

    def wrapper(return_something):

        for items in msg:

            x = return_something()

       

            if items == type(x):

                print("hello function is of the type expected")

                break

               

            else:

                print("Type Unexpected Error")

                                                   

    return wrapper

   

@expected_type(str,dict,set,list)

def return_something():

    return ('hello everyone')

# calling the function

print(return_something)

```

---

<div class="post-metadata">

**Author:** ![Jagaya](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jagaya/32/202277_2.png) [@Jagaya](https://forum.freecodecamp.org/u/Jagaya)\
**Post date:** [January 2, 2022, 12:10pm UTC](https://forum.freecodecamp.org/t/unexpected-type-error-decorator-exercise/490838/4 "2022-01-02T12:10:31Z")

</div>

> [@Jagmeet](#):
>
> ```auto
> # calling the function
> 
> print(return_something)
> 
> ```

That’s not how you call a function?

> [@Jagmeet](#):
>
> ```auto
> for items in msg:
> x = return_something()
> if items == type(x):
> 
> ```

That’s not how you compare an item to a list.  
For a start, a for loop will run a set number of times. If you give it 4 items, it will run up to 4 times - think about in what situation it would run 4 times and what it would put out.  
And when you are finished and before you rethink everything, just use “if-in”, because that’s the Python way of doing this.

---

<div class="post-metadata">

**Author:** ![kylec](https://avatars.discourse-cdn.com/v4/letter/k/c4cdca/32.png) [@kylec](https://forum.freecodecamp.org/u/kylec)\
**Post date:** [January 2, 2022, 10:08pm UTC](https://forum.freecodecamp.org/t/unexpected-type-error-decorator-exercise/490838/5 "2022-01-02T22:08:16Z")

</div>

Jumping off @sanity’s comment, I think you are not getting the result you expect because things are not being called _when_ you expect them. You can use a Python REPL to run your code in 3 steps (defining `expected_type`, defining `return_something`, and calling `return_something`), in order to see when exactly things are happening. Is `wrapper` being ran when `return_something` is called or during the definition of it?

---

<div class="post-metadata">

**Author:** ![Jagmeet](https://avatars.discourse-cdn.com/v4/letter/j/bc79bd/32.png) [@Jagmeet](https://forum.freecodecamp.org/u/Jagmeet)\
**Post date:** [January 6, 2022, 6:47pm UTC](https://forum.freecodecamp.org/t/unexpected-type-error-decorator-exercise/490838/6 "2022-01-06T18:47:16Z")

</div>

```auto

#the decorator creation

def expected_type(*msg):

    def wrapper(return_something):

        for items in msg:

            x = return_something()

       

            if items == type(x):

                print("hello function is of the type expected")

                break

               

            else:

                print("Type Unexpected Error")

                                                   

    return wrapper

   

@expected_type(str,dict,set,list)

def return_something():

    return ('hello everyone')

# calling the function

return_something()

```

In line 25 of the code l am getting error that:

TypeError: ‘NoneType’ object is not callable

Why does this error come up when the return something function returns something?

@kylec You are right about what you said, how do l run the code in 3 steps using the REPL

Thanks

---

<div class="post-metadata">

**Author:** ![Jagaya](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jagaya/32/202277_2.png) [@Jagaya](https://forum.freecodecamp.org/u/Jagaya)\
**Post date:** [January 6, 2022, 6:55pm UTC](https://forum.freecodecamp.org/t/unexpected-type-error-decorator-exercise/490838/7 "2022-01-06T18:55:32Z")

</div>

> [@Jagmeet](#):
>
> In line 25 of the code

I am not the best at counting, so would you kindly tell us which line that is?

---

<div class="post-metadata">

**Author:** ![sanity](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sanity/32/378897_2.png) [@sanity](https://forum.freecodecamp.org/u/sanity)\
**Post date:** [January 6, 2022, 7:24pm UTC](https://forum.freecodecamp.org/t/unexpected-type-error-decorator-exercise/490838/8 "2022-01-06T19:24:02Z")

</div>

`return_something` returns something before it’s decorated, but what returns decorated version of it?

Decorating `return_something` function with `expected_type(str,dict,set,list)` can be written as:

```python
return_something = expected_type(str,dict,set,list)(return_something)

```

`expected_type(str,dict,set,list)` returns `wrapper`, so it’s:

```python
return_something = wrapper(return_something)

```

So at the end `return_something` is whatever is returned by `wrapper` after passing it `return_something` function. Questions for this arises - what `wrapper` returns and can it be called, so we can still write `return_something()`.

---

<div class="post-metadata">

**Author:** ![Jagmeet](https://avatars.discourse-cdn.com/v4/letter/j/bc79bd/32.png) [@Jagmeet](https://forum.freecodecamp.org/u/Jagmeet)\
**Post date:** [January 7, 2022, 2:30am UTC](https://forum.freecodecamp.org/t/unexpected-type-error-decorator-exercise/490838/9 "2022-01-07T02:30:18Z")

</div>

> [@Jagmeet](#):
>
> ```auto
> return_something()
> 
> ```

@ Jagaya This is the line

Thanks

---

<div class="post-metadata">

**Author:** ![Jagmeet](https://avatars.discourse-cdn.com/v4/letter/j/bc79bd/32.png) [@Jagmeet](https://forum.freecodecamp.org/u/Jagmeet)\
**Post date:** [January 7, 2022, 2:34am UTC](https://forum.freecodecamp.org/t/unexpected-type-error-decorator-exercise/490838/10 "2022-01-07T02:34:33Z")

</div>

Ok I understand now, when l was looking at decorators, I also found this piece of code, that is almost the same in the structure, like inside the wrapper function

```auto
# defining a decorator
def hello_decorator(func):

	# inner1 is a Wrapper function in
	# which the argument is called
	
	# inner function can access the outer local
	# functions like in this case "func"
	def inner1():
		print("Hello, this is before function execution")

		# calling the actual function now
		# inside the wrapper function.
		func()

		print("This is after function execution")
		
	return inner1

# defining a function, to be called inside wrapper
def function_to_be_used():
	print("This is inside the function !!")

# passing 'function_to_be_used' inside the
# decorator to control its behaviour
function_to_be_used = hello_decorator(function_to_be_used)

# calling the function
function_to_be_used()

```

the source of this is here:

> **[Decorators in Python - GeeksforGeeks](https://www.geeksforgeeks.org/decorators-in-python/)**
>
> A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Thanks

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [July 8, 2022, 2:35pm UTC](https://forum.freecodecamp.org/t/unexpected-type-error-decorator-exercise/490838/11 "2022-07-08T14:35:11Z")

</div>

Closed on July 8, 2022, 2:35pm UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
