Github user dgingrich commented on a diff in the pull request: https://github.com/apache/spark/pull/17227#discussion_r123104696 --- Diff: python/pyspark/sql/tests.py --- @@ -2367,6 +2380,151 @@ def range_frame_match(): importlib.reload(window) + +class TypesTest(unittest.TestCase): + + def test_verify_type_ok_nullable(self): + for obj, data_type in [ + (None, IntegerType()), + (None, FloatType()), + (None, StringType()), + (None, StructType([]))]: + _verify_type(obj, data_type, nullable=True) + msg = "_verify_type(%s, %s, nullable=True)" % (obj, data_type) + self.assertTrue(True, msg) + + def test_verify_type_not_nullable(self): + import array + import datetime + import decimal + + MyStructType = StructType([ + StructField('s', StringType(), nullable=False), + StructField('i', IntegerType(), nullable=True)]) + + class MyObj: + def __init__(self, **ka): + for k, v in ka.items(): + setattr(self, k, v) + + # obj, data_type, exception (None for success or Exception subclass for error) + spec = [ + # Strings (match anything but None) + ("", StringType(), None), + (u"", StringType(), None), + (1, StringType(), None), + (1.0, StringType(), None), + ([], StringType(), None), + ({}, StringType(), None), + (None, StringType(), ValueError), # Only None test + + # UDT + (ExamplePoint(1.0, 2.0), ExamplePointUDT(), None), + (ExamplePoint(1.0, 2.0), PythonOnlyUDT(), ValueError), + + # Boolean + (True, BooleanType(), None), + (1, BooleanType(), TypeError), + ("True", BooleanType(), TypeError), + ([1], BooleanType(), TypeError), + + # Bytes + (-(2**7) - 1, ByteType(), ValueError), + (-(2**7), ByteType(), None), + (2**7 - 1, ByteType(), None), + (2**7, ByteType(), ValueError), + ("1", ByteType(), TypeError), + (1.0, ByteType(), TypeError), + + # Shorts + (-(2**15) - 1, ShortType(), ValueError), + (-(2**15), ShortType(), None), + (2**15 - 1, ShortType(), None), + (2**15, ShortType(), ValueError), + + # Integer + (-(2**31) - 1, IntegerType(), ValueError), + (-(2**31), IntegerType(), None), + (2**31 - 1, IntegerType(), None), + (2**31, IntegerType(), ValueError), + + # Long + (2**64, LongType(), None), + + # Float & Double + (1.0, FloatType(), None), + (1, FloatType(), TypeError), + (1.0, DoubleType(), None), + (1, DoubleType(), TypeError), + + # Decimal + (decimal.Decimal("1.0"), DecimalType(), None), + (1.0, DecimalType(), TypeError), + (1, DecimalType(), TypeError), + ("1.0", DecimalType(), TypeError), + + # Binary + (bytearray([1, 2]), BinaryType(), None), + (1, BinaryType(), TypeError), + + # Date/Time + (datetime.date(2000, 1, 2), DateType(), None), + (datetime.datetime(2000, 1, 2, 3, 4), DateType(), None), + ("2000-01-02", DateType(), TypeError), + (datetime.datetime(2000, 1, 2, 3, 4), TimestampType(), None), + (946811040, TimestampType(), TypeError), + + # Array + ([], ArrayType(IntegerType()), None), + (["1", None], ArrayType(StringType(), containsNull=True), None), + ([1, 2], ArrayType(IntegerType()), None), + ([1, "2"], ArrayType(IntegerType()), TypeError), + ((1, 2), ArrayType(IntegerType()), None), + (array.array('h', [1, 2]), ArrayType(IntegerType()), None), + + # Map + ({}, MapType(StringType(), IntegerType()), None), + ({"a": 1}, MapType(StringType(), IntegerType()), None), + ({"a": 1}, MapType(IntegerType(), IntegerType()), TypeError), + ({"a": "1"}, MapType(StringType(), IntegerType()), TypeError), + ({"a": None}, MapType(StringType(), IntegerType(), valueContainsNull=True), None), + + # Struct + ({"s": "a", "i": 1}, MyStructType, None), + ({"s": "a", "i": None}, MyStructType, None), + ({"s": "a"}, MyStructType, None), + ({"s": "a", "f": 1.0}, MyStructType, None), # Extra fields OK + ({"s": "a", "i": "1"}, MyStructType, TypeError), + (Row(s="a", i=1), MyStructType, None), + (Row(s="a", i=None), MyStructType, None), + (Row(s="a", i=1, f=1.0), MyStructType, None), # Extra fields OK + (Row(s="a"), MyStructType, ValueError), # Row can't have missing field + (Row(s="a", i="1"), MyStructType, TypeError), + (["a", 1], MyStructType, None), + (["a", None], MyStructType, None), + (["a"], MyStructType, ValueError), + (["a", "1"], MyStructType, TypeError), + (("a", 1), MyStructType, None), + (MyObj(s="a", i=1), MyStructType, None), + (MyObj(s="a", i=None), MyStructType, None), + (MyObj(s="a"), MyStructType, None), + (MyObj(s="a", i="1"), MyStructType, TypeError), + ] + + for obj, data_type, exp in spec: + msg = "_verify_type(%s, %s, nullable=False) == %s" % (obj, data_type, exp) + if exp is None: + try: + _verify_type(obj, data_type, nullable=False) + except Exception as e: + traceback.print_exc() + self.fail(msg) + self.assertTrue(True, msg) --- End diff -- IIRC that was required at some point, I think to get a test runner to pick up the test. But I just tried removing it and tests ran with pytest and the Python3 unittest runner, so removed the line here and in `test_verify_type_ok_nullable`. We should probably look at the full test suite output from Jenkins to make sure the tests are run under Python2.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org