6 kyu

Expected Type Decorator

Description
Loading description...
Fundamentals
Design Patterns
Object-oriented Programming
  • Please sign in or sign up to leave a comment.
  • DallogFheir Avatar

    This comment has been hidden.

  • Maratik375 Avatar

    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

  • Danil Tolmachov Avatar
    Traceback (most recent call last):
      File "tests.py", line 18, in <module>
        return_something(None)
      File "/workspace/default/solution.py", line 5, in inner2
        raise UnexpectedTypeException
    NameError: name 'UnexpectedTypeException' is not defined
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "tests.py", line 20, in <module>
        except UnexpectedTypeException:
    NameError: name 'UnexpectedTypeException' is not defined
    

    I think Python test is broken. (I'm tried to raise UnexpectedTypeException and another errors but nothing really worked)

  • llewis257 Avatar

    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?

  • Wei-Ting Yang Avatar

    A comma is missed in the docstring.

    def expected_type(return_types):
        """
        ...
        @expected_type((str))  # should be @expected_type((str, ))
        ...
    
  • FArekkusu Avatar
  • alcoholfreebear Avatar

    The example instruction is misleading.

    >>> return_something('The quick brown fox jumps over the lazy dog.')
        'The quick brown fox jumps over the lazy dog.'
    >>> return_something('The quick brown fox jumps over the lazy dog.')
        'Maybe you'll output another string...'
    

    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.

  • maverickx Avatar

    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?

  • RealKenshiro Avatar

    Very instructive!

  • srgrumyantsev Avatar

    Very interesting kata. I feel like I've learned a lot solving it.

  • Fbasham Avatar

    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.

  • FArekkusu Avatar

    There's a sample test where expected_type receives a list, but no such fixed/random tests.

  • 0bart Avatar

    Why in this test exception should not be raised?

    return_types = (<class 'dict'>,) 
    return_something's output = Counter({'e': 2, 'r': 1})  # type(output) = <class 'collections.Counter'>
    

    Counter class is a dict subclass, not dict in itself.

    The same in random tests:

    return_types = (<class 'tuple'>, <class 'collections.abc.Iterable'>, <class 'set'>, <class 'str'>, <class 'function'>, <class 'dict'>, <class 'collections.deque'>, <class 'int'>)
    return_something's output = Counter({'d': 2, 'r': 1, 't': 1, 's': 1, 'g': 1})  # type(output) = <class 'collections.Counter'>
    

    According to tests, exception also should not be raised.

    Why?

  • TimSonrisa Avatar

    In the Python Version the 1st sample test passes a list and not a tuple as described in the specifications.

  • TimSonrisa Avatar

    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/

  • pmcf Avatar

    A tough kata, learned a lot about decorator functions!

  • docgunthrop Avatar

    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 ✌️

  • reshawn Avatar

    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.

  • Blind4Basics Avatar
    • added random tests
    • changed the input type of the decorator to something more natural considering the task: tuple
    • added "a lot" of edge cases
    • ...approved.
  • Voile Avatar

    Needs random tests

  • siebenschlaefer Avatar

    This comment has been hidden.

  • adamb Avatar

    There are not enough test cases:

    • Test cases for functions taking more than 1 arguments are needed
    • Because of the lack of proper test cases, solutions checking input argument (insted of the output) are accepted
  • Unnamed Avatar

    In the example test cases the expected exception is called InvalidTypeException instead of UnexpectedTypeException.