mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-28 10:28:06 +00:00
Use correct default type for str (#8623)
* [Python] Use correct type for str with None Otherwise mypy will correctly flag code like this def __init__(self): self.fooBar = None # type: Optional[str] error: Incompatible types in assignment (expression has type "None", variable has type "str") * [Python] Make list type optional as they can contain None
This commit is contained in:
@@ -1628,7 +1628,7 @@ class PythonGenerator : public BaseGenerator {
|
|||||||
} else if (IsInteger(base_type)) {
|
} else if (IsInteger(base_type)) {
|
||||||
return "int";
|
return "int";
|
||||||
} else if (base_type == BASE_TYPE_STRING) {
|
} else if (base_type == BASE_TYPE_STRING) {
|
||||||
return "str";
|
return "Optional[str]";
|
||||||
} else {
|
} else {
|
||||||
FLATBUFFERS_ASSERT(false && "base_type is not a scalar or string type.");
|
FLATBUFFERS_ASSERT(false && "base_type is not a scalar or string type.");
|
||||||
return "";
|
return "";
|
||||||
@@ -1726,10 +1726,10 @@ class PythonGenerator : public BaseGenerator {
|
|||||||
field_type = package_reference + "." + object_type + "]";
|
field_type = package_reference + "." + object_type + "]";
|
||||||
import_list->insert("import " + package_reference);
|
import_list->insert("import " + package_reference);
|
||||||
}
|
}
|
||||||
field_type = "List[" + field_type;
|
field_type = "Optional[List[" + field_type + "]";
|
||||||
} else {
|
} else {
|
||||||
field_type =
|
field_type =
|
||||||
"List[" + GetBasePythonTypeForScalarAndString(base_type) + "]";
|
"Optional[List[" + GetBasePythonTypeForScalarAndString(base_type) + "]]";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1769,7 +1769,7 @@ class PythonGenerator : public BaseGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const auto default_value = GetDefaultValue(field);
|
const auto default_value = GetDefaultValue(field);
|
||||||
// Wrties the init statement.
|
// Writes the init statement.
|
||||||
const auto field_field = namer_.Field(field);
|
const auto field_field = namer_.Field(field);
|
||||||
code += GenIndents(2) + "self." + field_field + " = " + default_value +
|
code += GenIndents(2) + "self." + field_field + " = " + default_value +
|
||||||
" # type: " + field_type;
|
" # type: " + field_type;
|
||||||
|
|||||||
@@ -116,11 +116,11 @@ class ArrayStructT(object):
|
|||||||
# ArrayStructT
|
# ArrayStructT
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.a = 0.0 # type: float
|
self.a = 0.0 # type: float
|
||||||
self.b = None # type: List[int]
|
self.b = None # type: Optional[List[int]]
|
||||||
self.c = 0 # type: int
|
self.c = 0 # type: int
|
||||||
self.d = None # type: List[MyGame.Example.NestedStruct.NestedStructT]
|
self.d = None # type: Optional[List[MyGame.Example.NestedStruct.NestedStructT]]
|
||||||
self.e = 0 # type: int
|
self.e = 0 # type: int
|
||||||
self.f = None # type: List[int]
|
self.f = None # type: Optional[List[int]]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def InitFromBuf(cls, buf, pos):
|
def InitFromBuf(cls, buf, pos):
|
||||||
|
|||||||
@@ -1407,16 +1407,16 @@ class MonsterT(object):
|
|||||||
self.pos = None # type: Optional[MyGame.Example.Vec3.Vec3T]
|
self.pos = None # type: Optional[MyGame.Example.Vec3.Vec3T]
|
||||||
self.mana = 150 # type: int
|
self.mana = 150 # type: int
|
||||||
self.hp = 100 # type: int
|
self.hp = 100 # type: int
|
||||||
self.name = None # type: str
|
self.name = None # type: Optional[str]
|
||||||
self.inventory = None # type: List[int]
|
self.inventory = None # type: Optional[List[int]]
|
||||||
self.color = 8 # type: int
|
self.color = 8 # type: int
|
||||||
self.testType = 0 # type: int
|
self.testType = 0 # type: int
|
||||||
self.test = None # type: Union[None, MyGame.Example.Monster.MonsterT, MyGame.Example.TestSimpleTableWithEnum.TestSimpleTableWithEnumT, MyGame.Example2.Monster.MonsterT]
|
self.test = None # type: Union[None, MyGame.Example.Monster.MonsterT, MyGame.Example.TestSimpleTableWithEnum.TestSimpleTableWithEnumT, MyGame.Example2.Monster.MonsterT]
|
||||||
self.test4 = None # type: List[MyGame.Example.Test.TestT]
|
self.test4 = None # type: Optional[List[MyGame.Example.Test.TestT]]
|
||||||
self.testarrayofstring = None # type: List[str]
|
self.testarrayofstring = None # type: Optional[List[Optional[str]]]
|
||||||
self.testarrayoftables = None # type: List[MyGame.Example.Monster.MonsterT]
|
self.testarrayoftables = None # type: Optional[List[MyGame.Example.Monster.MonsterT]]
|
||||||
self.enemy = None # type: Optional[MyGame.Example.Monster.MonsterT]
|
self.enemy = None # type: Optional[MyGame.Example.Monster.MonsterT]
|
||||||
self.testnestedflatbuffer = None # type: List[int]
|
self.testnestedflatbuffer = None # type: Optional[List[int]]
|
||||||
self.testempty = None # type: Optional[MyGame.Example.Stat.StatT]
|
self.testempty = None # type: Optional[MyGame.Example.Stat.StatT]
|
||||||
self.testbool = False # type: bool
|
self.testbool = False # type: bool
|
||||||
self.testhashs32Fnv1 = 0 # type: int
|
self.testhashs32Fnv1 = 0 # type: int
|
||||||
@@ -1427,33 +1427,33 @@ class MonsterT(object):
|
|||||||
self.testhashu32Fnv1a = 0 # type: int
|
self.testhashu32Fnv1a = 0 # type: int
|
||||||
self.testhashs64Fnv1a = 0 # type: int
|
self.testhashs64Fnv1a = 0 # type: int
|
||||||
self.testhashu64Fnv1a = 0 # type: int
|
self.testhashu64Fnv1a = 0 # type: int
|
||||||
self.testarrayofbools = None # type: List[bool]
|
self.testarrayofbools = None # type: Optional[List[bool]]
|
||||||
self.testf = 3.14159 # type: float
|
self.testf = 3.14159 # type: float
|
||||||
self.testf2 = 3.0 # type: float
|
self.testf2 = 3.0 # type: float
|
||||||
self.testf3 = 0.0 # type: float
|
self.testf3 = 0.0 # type: float
|
||||||
self.testarrayofstring2 = None # type: List[str]
|
self.testarrayofstring2 = None # type: Optional[List[Optional[str]]]
|
||||||
self.testarrayofsortedstruct = None # type: List[MyGame.Example.Ability.AbilityT]
|
self.testarrayofsortedstruct = None # type: Optional[List[MyGame.Example.Ability.AbilityT]]
|
||||||
self.flex = None # type: List[int]
|
self.flex = None # type: Optional[List[int]]
|
||||||
self.test5 = None # type: List[MyGame.Example.Test.TestT]
|
self.test5 = None # type: Optional[List[MyGame.Example.Test.TestT]]
|
||||||
self.vectorOfLongs = None # type: List[int]
|
self.vectorOfLongs = None # type: Optional[List[int]]
|
||||||
self.vectorOfDoubles = None # type: List[float]
|
self.vectorOfDoubles = None # type: Optional[List[float]]
|
||||||
self.parentNamespaceTest = None # type: Optional[MyGame.InParentNamespace.InParentNamespaceT]
|
self.parentNamespaceTest = None # type: Optional[MyGame.InParentNamespace.InParentNamespaceT]
|
||||||
self.vectorOfReferrables = None # type: List[MyGame.Example.Referrable.ReferrableT]
|
self.vectorOfReferrables = None # type: Optional[List[MyGame.Example.Referrable.ReferrableT]]
|
||||||
self.singleWeakReference = 0 # type: int
|
self.singleWeakReference = 0 # type: int
|
||||||
self.vectorOfWeakReferences = None # type: List[int]
|
self.vectorOfWeakReferences = None # type: Optional[List[int]]
|
||||||
self.vectorOfStrongReferrables = None # type: List[MyGame.Example.Referrable.ReferrableT]
|
self.vectorOfStrongReferrables = None # type: Optional[List[MyGame.Example.Referrable.ReferrableT]]
|
||||||
self.coOwningReference = 0 # type: int
|
self.coOwningReference = 0 # type: int
|
||||||
self.vectorOfCoOwningReferences = None # type: List[int]
|
self.vectorOfCoOwningReferences = None # type: Optional[List[int]]
|
||||||
self.nonOwningReference = 0 # type: int
|
self.nonOwningReference = 0 # type: int
|
||||||
self.vectorOfNonOwningReferences = None # type: List[int]
|
self.vectorOfNonOwningReferences = None # type: Optional[List[int]]
|
||||||
self.anyUniqueType = 0 # type: int
|
self.anyUniqueType = 0 # type: int
|
||||||
self.anyUnique = None # type: Union[None, MyGame.Example.Monster.MonsterT, MyGame.Example.TestSimpleTableWithEnum.TestSimpleTableWithEnumT, MyGame.Example2.Monster.MonsterT]
|
self.anyUnique = None # type: Union[None, MyGame.Example.Monster.MonsterT, MyGame.Example.TestSimpleTableWithEnum.TestSimpleTableWithEnumT, MyGame.Example2.Monster.MonsterT]
|
||||||
self.anyAmbiguousType = 0 # type: int
|
self.anyAmbiguousType = 0 # type: int
|
||||||
self.anyAmbiguous = None # type: Union[None, MyGame.Example.Monster.MonsterT, MyGame.Example.Monster.MonsterT, MyGame.Example.Monster.MonsterT]
|
self.anyAmbiguous = None # type: Union[None, MyGame.Example.Monster.MonsterT, MyGame.Example.Monster.MonsterT, MyGame.Example.Monster.MonsterT]
|
||||||
self.vectorOfEnums = None # type: List[int]
|
self.vectorOfEnums = None # type: Optional[List[int]]
|
||||||
self.signedEnum = -1 # type: int
|
self.signedEnum = -1 # type: int
|
||||||
self.testrequirednestedflatbuffer = None # type: List[int]
|
self.testrequirednestedflatbuffer = None # type: Optional[List[int]]
|
||||||
self.scalarKeySortedTables = None # type: List[MyGame.Example.Stat.StatT]
|
self.scalarKeySortedTables = None # type: Optional[List[MyGame.Example.Stat.StatT]]
|
||||||
self.nativeInline = None # type: Optional[MyGame.Example.Test.TestT]
|
self.nativeInline = None # type: Optional[MyGame.Example.Test.TestT]
|
||||||
self.longEnumNonEnumDefault = 0 # type: int
|
self.longEnumNonEnumDefault = 0 # type: int
|
||||||
self.longEnumNormalDefault = 2 # type: int
|
self.longEnumNormalDefault = 2 # type: int
|
||||||
|
|||||||
@@ -105,10 +105,10 @@ class NestedStructT(object):
|
|||||||
|
|
||||||
# NestedStructT
|
# NestedStructT
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.a = None # type: List[int]
|
self.a = None # type: Optional[List[int]]
|
||||||
self.b = 0 # type: int
|
self.b = 0 # type: int
|
||||||
self.c = None # type: List[int]
|
self.c = None # type: Optional[List[int]]
|
||||||
self.d = None # type: List[int]
|
self.d = None # type: Optional[List[int]]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def InitFromBuf(cls, buf, pos):
|
def InitFromBuf(cls, buf, pos):
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ class NestedUnionTestT(object):
|
|||||||
|
|
||||||
# NestedUnionTestT
|
# NestedUnionTestT
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.name = None # type: str
|
self.name = None # type: Optional[str]
|
||||||
self.dataType = 0 # type: int
|
self.dataType = 0 # type: int
|
||||||
self.data = None # type: Union[None, MyGame.Example.NestedUnion.Vec3.Vec3T, MyGame.Example.NestedUnion.TestSimpleTableWithEnum.TestSimpleTableWithEnumT]
|
self.data = None # type: Union[None, MyGame.Example.NestedUnion.Vec3.Vec3T, MyGame.Example.NestedUnion.TestSimpleTableWithEnum.TestSimpleTableWithEnumT]
|
||||||
self.id = 0 # type: int
|
self.id = 0 # type: int
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ class StatT(object):
|
|||||||
|
|
||||||
# StatT
|
# StatT
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.id = None # type: str
|
self.id = None # type: Optional[str]
|
||||||
self.val = 0 # type: int
|
self.val = 0 # type: int
|
||||||
self.count = 0 # type: int
|
self.count = 0 # type: int
|
||||||
|
|
||||||
|
|||||||
@@ -267,8 +267,8 @@ class TypeAliasesT(object):
|
|||||||
self.u64 = 0 # type: int
|
self.u64 = 0 # type: int
|
||||||
self.f32 = 0.0 # type: float
|
self.f32 = 0.0 # type: float
|
||||||
self.f64 = 0.0 # type: float
|
self.f64 = 0.0 # type: float
|
||||||
self.v8 = None # type: List[int]
|
self.v8 = None # type: Optional[List[int]]
|
||||||
self.vf64 = None # type: List[float]
|
self.vf64 = None # type: Optional[List[float]]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def InitFromBuf(cls, buf, pos):
|
def InitFromBuf(cls, buf, pos):
|
||||||
|
|||||||
@@ -240,8 +240,8 @@ class MonsterExtraT(object):
|
|||||||
self.f1 = float('nan') # type: float
|
self.f1 = float('nan') # type: float
|
||||||
self.f2 = float('inf') # type: float
|
self.f2 = float('inf') # type: float
|
||||||
self.f3 = float('-inf') # type: float
|
self.f3 = float('-inf') # type: float
|
||||||
self.dvec = None # type: List[float]
|
self.dvec = None # type: Optional[List[float]]
|
||||||
self.fvec = None # type: List[float]
|
self.fvec = None # type: Optional[List[float]]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def InitFromBuf(cls, buf, pos):
|
def InitFromBuf(cls, buf, pos):
|
||||||
|
|||||||
@@ -728,7 +728,7 @@ class StatT(object):
|
|||||||
|
|
||||||
# StatT
|
# StatT
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.id = None # type: str
|
self.id = None # type: Optional[str]
|
||||||
self.val = 0 # type: int
|
self.val = 0 # type: int
|
||||||
self.count = 0 # type: int
|
self.count = 0 # type: int
|
||||||
|
|
||||||
@@ -1973,16 +1973,16 @@ class MonsterT(object):
|
|||||||
self.pos = None # type: Optional[Vec3T]
|
self.pos = None # type: Optional[Vec3T]
|
||||||
self.mana = 150 # type: int
|
self.mana = 150 # type: int
|
||||||
self.hp = 100 # type: int
|
self.hp = 100 # type: int
|
||||||
self.name = None # type: str
|
self.name = None # type: Optional[str]
|
||||||
self.inventory = None # type: List[int]
|
self.inventory = None # type: Optional[List[int]]
|
||||||
self.color = 8 # type: int
|
self.color = 8 # type: int
|
||||||
self.testType = 0 # type: int
|
self.testType = 0 # type: int
|
||||||
self.test = None # type: Union[None, MonsterT, TestSimpleTableWithEnumT, MonsterT]
|
self.test = None # type: Union[None, MonsterT, TestSimpleTableWithEnumT, MonsterT]
|
||||||
self.test4 = None # type: List[TestT]
|
self.test4 = None # type: Optional[List[TestT]]
|
||||||
self.testarrayofstring = None # type: List[str]
|
self.testarrayofstring = None # type: Optional[List[Optional[str]]]
|
||||||
self.testarrayoftables = None # type: List[MonsterT]
|
self.testarrayoftables = None # type: Optional[List[MonsterT]]
|
||||||
self.enemy = None # type: Optional[MonsterT]
|
self.enemy = None # type: Optional[MonsterT]
|
||||||
self.testnestedflatbuffer = None # type: List[int]
|
self.testnestedflatbuffer = None # type: Optional[List[int]]
|
||||||
self.testempty = None # type: Optional[StatT]
|
self.testempty = None # type: Optional[StatT]
|
||||||
self.testbool = False # type: bool
|
self.testbool = False # type: bool
|
||||||
self.testhashs32Fnv1 = 0 # type: int
|
self.testhashs32Fnv1 = 0 # type: int
|
||||||
@@ -1993,33 +1993,33 @@ class MonsterT(object):
|
|||||||
self.testhashu32Fnv1a = 0 # type: int
|
self.testhashu32Fnv1a = 0 # type: int
|
||||||
self.testhashs64Fnv1a = 0 # type: int
|
self.testhashs64Fnv1a = 0 # type: int
|
||||||
self.testhashu64Fnv1a = 0 # type: int
|
self.testhashu64Fnv1a = 0 # type: int
|
||||||
self.testarrayofbools = None # type: List[bool]
|
self.testarrayofbools = None # type: Optional[List[bool]]
|
||||||
self.testf = 3.14159 # type: float
|
self.testf = 3.14159 # type: float
|
||||||
self.testf2 = 3.0 # type: float
|
self.testf2 = 3.0 # type: float
|
||||||
self.testf3 = 0.0 # type: float
|
self.testf3 = 0.0 # type: float
|
||||||
self.testarrayofstring2 = None # type: List[str]
|
self.testarrayofstring2 = None # type: Optional[List[Optional[str]]]
|
||||||
self.testarrayofsortedstruct = None # type: List[AbilityT]
|
self.testarrayofsortedstruct = None # type: Optional[List[AbilityT]]
|
||||||
self.flex = None # type: List[int]
|
self.flex = None # type: Optional[List[int]]
|
||||||
self.test5 = None # type: List[TestT]
|
self.test5 = None # type: Optional[List[TestT]]
|
||||||
self.vectorOfLongs = None # type: List[int]
|
self.vectorOfLongs = None # type: Optional[List[int]]
|
||||||
self.vectorOfDoubles = None # type: List[float]
|
self.vectorOfDoubles = None # type: Optional[List[float]]
|
||||||
self.parentNamespaceTest = None # type: Optional[InParentNamespaceT]
|
self.parentNamespaceTest = None # type: Optional[InParentNamespaceT]
|
||||||
self.vectorOfReferrables = None # type: List[ReferrableT]
|
self.vectorOfReferrables = None # type: Optional[List[ReferrableT]]
|
||||||
self.singleWeakReference = 0 # type: int
|
self.singleWeakReference = 0 # type: int
|
||||||
self.vectorOfWeakReferences = None # type: List[int]
|
self.vectorOfWeakReferences = None # type: Optional[List[int]]
|
||||||
self.vectorOfStrongReferrables = None # type: List[ReferrableT]
|
self.vectorOfStrongReferrables = None # type: Optional[List[ReferrableT]]
|
||||||
self.coOwningReference = 0 # type: int
|
self.coOwningReference = 0 # type: int
|
||||||
self.vectorOfCoOwningReferences = None # type: List[int]
|
self.vectorOfCoOwningReferences = None # type: Optional[List[int]]
|
||||||
self.nonOwningReference = 0 # type: int
|
self.nonOwningReference = 0 # type: int
|
||||||
self.vectorOfNonOwningReferences = None # type: List[int]
|
self.vectorOfNonOwningReferences = None # type: Optional[List[int]]
|
||||||
self.anyUniqueType = 0 # type: int
|
self.anyUniqueType = 0 # type: int
|
||||||
self.anyUnique = None # type: Union[None, MonsterT, TestSimpleTableWithEnumT, MonsterT]
|
self.anyUnique = None # type: Union[None, MonsterT, TestSimpleTableWithEnumT, MonsterT]
|
||||||
self.anyAmbiguousType = 0 # type: int
|
self.anyAmbiguousType = 0 # type: int
|
||||||
self.anyAmbiguous = None # type: Union[None, MonsterT, MonsterT, MonsterT]
|
self.anyAmbiguous = None # type: Union[None, MonsterT, MonsterT, MonsterT]
|
||||||
self.vectorOfEnums = None # type: List[int]
|
self.vectorOfEnums = None # type: Optional[List[int]]
|
||||||
self.signedEnum = -1 # type: int
|
self.signedEnum = -1 # type: int
|
||||||
self.testrequirednestedflatbuffer = None # type: List[int]
|
self.testrequirednestedflatbuffer = None # type: Optional[List[int]]
|
||||||
self.scalarKeySortedTables = None # type: List[StatT]
|
self.scalarKeySortedTables = None # type: Optional[List[StatT]]
|
||||||
self.nativeInline = None # type: Optional[TestT]
|
self.nativeInline = None # type: Optional[TestT]
|
||||||
self.longEnumNonEnumDefault = 0 # type: int
|
self.longEnumNonEnumDefault = 0 # type: int
|
||||||
self.longEnumNormalDefault = 2 # type: int
|
self.longEnumNormalDefault = 2 # type: int
|
||||||
@@ -2719,8 +2719,8 @@ class TypeAliasesT(object):
|
|||||||
self.u64 = 0 # type: int
|
self.u64 = 0 # type: int
|
||||||
self.f32 = 0.0 # type: float
|
self.f32 = 0.0 # type: float
|
||||||
self.f64 = 0.0 # type: float
|
self.f64 = 0.0 # type: float
|
||||||
self.v8 = None # type: List[int]
|
self.v8 = None # type: Optional[List[int]]
|
||||||
self.vf64 = None # type: List[float]
|
self.vf64 = None # type: Optional[List[float]]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def InitFromBuf(cls, buf, pos):
|
def InitFromBuf(cls, buf, pos):
|
||||||
|
|||||||
Reference in New Issue
Block a user