6 kyu
Expected Type Decorator
Loading description...
Fundamentals
Design Patterns
Object-oriented Programming
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
This comment has been hidden.
One test in "handle inheritance correctly" says that Test Passed when nothing is returned and the exception is not raised while the res of function is "string is one", type of res is "<class 'str'>" and expected type is "<class 'collections.abc.Iterable'>". I think in such a case it should raise UnexpectedTypeException 'cause str is not collections.abc.Iterable', but then it says that the test is not passed.
P.S. Sorry for my clumsy English
I understood. The idea is that "<class 'collections.abc.Iterable'>" is subclass of "<class 'str'>" so type of "<class 'collections.abc.Iterable'>" is equel to type of "<class 'str'>" in some way(as I understood).
I think Python test is broken. (I'm tried to raise UnexpectedTypeException and another errors but nothing really worked)
This is most presumably coming from your code. To raise an issue, you must prove there is an issue, see there: https://docs.codewars.com/training/troubleshooting/
I believe there are some tests that are wrong: a dict type is not supposed to rais an exception on collections.counter for instance. Any suggestion/clue of why that is?
I didn't solve this kata, but:
In my understanding of the kata, it is not the same type (though it can be subclass).
Unless I misunderstood your post: do you mean the kata expects no exception in such cases? If so, I agreee the description is lacking some specifications.
Yes,the kata seems to be expecting the 2 types to be the same and no exception should be raised which I found a bit odd.
warning, getting a Counter while expecting a dict shouldn't raise. On the other hand, getting a dict while expecting a Counter should definitely raise.
It's not explicit in the description, but there is a test section that is very clear about the intent:
@test.it("Handle inheritance correctly")
A comma is missed in the docstring.
Python 3.10 fork.
Approved (and removed from list)
The example instruction is misleading.
This example instruction indicates that by returning the same string twice the code should suggest to output another string. This means that the decorator needs to be a class that remembers all past inputs, but the tests in this kata does not require this feature. If you implement this feature according to instruction your tests will crash.
Hello!
I've got the following error: STDERR tests.py:3: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working from collections import deque,Counter,Iterable Traceback (most recent call last): File "tests.py", line 15, in def f(): File "/workspace/default/src/codewars-test/codewars_test/test_framework.py", line 111, in wrapper func() File "tests.py", line 31, in f def t(): File "/workspace/default/src/codewars-test/codewars_test/test_framework.py", line 111, in wrapper func() File "tests.py", line 37, in t test.assert_equals(return_something(45), 45) File "/workspace/default/solution.py", line 14, in wrapper raise UnexpectedTypeException solution.UnexpectedTypeException
But all three tests were passed Could someone help with this?
Very instructive!
Very interesting kata. I feel like I've learned a lot solving it.
Nice question. I was stuck forever because I didn't read the part about the return type not matching the specified types. Instead I thought it was the decorated function argument being checked.
There's a sample test where
expected_type
receives a list, but no such fixed/random tests.changed to tuple (I forgot to check the sample tests when I updated the kata, has almost usual...)
wait... you were talking about the type of the argument, or the types inside the argument??
Why in this test exception should not be raised?
Counter class is a dict subclass, not dict in itself.
The same in random tests:
According to tests, exception also should not be raised.
Why?
because Counter is subclass of dict..try isinstance(a,b) it will handle inheritance
In the Python Version the 1st sample test passes a list and not a tuple as described in the specifications.
Thank you for the Kata and the Python version, had to do some research on decorators and inheritance, but as an end result I feel I learned a lot today. Used this useful resource: https://www.thecodeship.com/patterns/guide-to-python-function-decorators/
A tough kata, learned a lot about decorator functions!
Nice work on the Python translation, @B4B. My solution is unnecessarily verbose (my mind's been on Kotlin lately) but I learned something new regardless ✌️
In the part " Other exceptions are thrown normally" of the fixed tests, I don't know why I can't pass and it always says "Value is not what was expected".
The type is "str" and the arg is None, as the rules, it should raise an UnexpectedTypeException.
nope, you misread the rules. I'll add some other tests, maybe.
I didn't add other tests, but I changed the assertion message to make it clear that this is the expected behavior.
however, the message is so unclear that I can't understand what's going on with this test. It seems like the example mentioned in the Details:
>>> return_something(None)
UnexpectedTypeException: Was expecting instance of: str
Maybe it is another different function, so confused.
You cannot be reasonning only on that part of the description, you have to considere what is the decorated function itself and what are the arguments for the decorator itself.
I'll change the description to something more meaningful, like:
Note: I just updated the random tests so that this behavior is enforced through them too.
I find I misunderstood this Kata. Very Sorry ...
no problem, it actually made the kata better! ;)
tuple
Needs random tests
done
This comment has been hidden.
done (sorry, I didn't even see your message before I updated the test cases... x) So... I did it my way ;) )
There are not enough test cases:
done
In the example test cases the expected exception is called
InvalidTypeException
instead ofUnexpectedTypeException
.Hey, thanks a ton! I've just fixed it. Thanks.