Skip to content

Commit a071175

Browse files
committed
Fix Ruff lint errors related to docstrings
See the relevant rules in the Ruff documentation: * [D205](https://docs.astral.sh/ruff/rules/missing-blank-line-after-summary/) * [D401](https://docs.astral.sh/ruff/rules/non-imperative-mood/) This commit fixes the following lint errors: ```console $ ruff --version ruff 0.14.10 $ ruff check --statistics --ignore E501 $(git ls-files '*.py') 16 D205 missing-blank-line-after-summary 3 D401 non-imperative-mood Found 19 errors. $ ruff check --ignore E501 $(git ls-files '*.py') D205 1 blank line required between summary line and description --> kaitaistruct.py:341:9 | 340 | def read_bits_int(self, n): 341 | / """Deprecated and no longer used as of KSC 0.9. It is only available 342 | | for backwards compatibility and will be removed in the future. 343 | | 344 | | KSC 0.9 and later uses `read_bits_int_be()` instead. 345 | | """ | |___________^ 346 | warnings.warn( 347 | "read_bits_int() is deprecated since 0.9, use read_bits_int_be() instead", | help: Insert single blank line D401 First line of docstring should be in imperative mood: "Deprecated and no longer used as of KSC 0.9. It is only available" --> kaitaistruct.py:341:9 | 340 | def read_bits_int(self, n): 341 | / """Deprecated and no longer used as of KSC 0.9. It is only available 342 | | for backwards compatibility and will be removed in the future. 343 | | 344 | | KSC 0.9 and later uses `read_bits_int_be()` instead. 345 | | """ | |___________^ 346 | warnings.warn( 347 | "read_bits_int() is deprecated since 0.9, use read_bits_int_be() instead", | D205 1 blank line required between summary line and description --> kaitaistruct.py:470:9 | 469 | def ensure_fixed_contents(self, expected): 470 | / """Deprecated and no longer used as of KSC 0.9. It is only available 471 | | for backwards compatibility and will be removed in the future. 472 | | 473 | | KSC 0.9 and later explicitly raises `ValidationNotEqualError` from an 474 | | `if` statement instead. 475 | | """ | |___________^ 476 | warnings.warn( 477 | "ensure_fixed_contents() is deprecated since 0.9, explicitly raise " | help: Insert single blank line D401 First line of docstring should be in imperative mood: "Deprecated and no longer used as of KSC 0.9. It is only available" --> kaitaistruct.py:470:9 | 469 | def ensure_fixed_contents(self, expected): 470 | / """Deprecated and no longer used as of KSC 0.9. It is only available 471 | | for backwards compatibility and will be removed in the future. 472 | | 473 | | KSC 0.9 and later explicitly raises `ValidationNotEqualError` from an 474 | | `if` statement instead. 475 | | """ | |___________^ 476 | warnings.warn( 477 | "ensure_fixed_contents() is deprecated since 0.9, explicitly raise " | D205 1 blank line required between summary line and description --> kaitaistruct.py:829:9 | 827 | @staticmethod 828 | def resolve_enum(enum_obj, value): 829 | / """Resolves value using enum: if the value is not found in the map, 830 | | we'll just use literal value per se. Works around problem with Python 831 | | enums throwing an exception when encountering unknown value. 832 | | """ | |___________^ 833 | try: 834 | return enum_obj(value) | help: Insert single blank line D401 First line of docstring should be in imperative mood: "Resolves value using enum: if the value is not found in the map," --> kaitaistruct.py:829:9 | 827 | @staticmethod 828 | def resolve_enum(enum_obj, value): 829 | / """Resolves value using enum: if the value is not found in the map, 830 | | we'll just use literal value per se. Works around problem with Python 831 | | enums throwing an exception when encountering unknown value. 832 | | """ | |___________^ 833 | try: 834 | return enum_obj(value) | D205 1 blank line required between summary line and description --> kaitaistruct.py:874:5 | 873 | class KaitaiStructError(Exception): 874 | / """Common ancestor for all errors originating from correct Kaitai Struct 875 | | usage (i.e. errors that indicate a problem with user input, not errors 876 | | indicating incorrect usage that are not meant to be caught but fixed in the 877 | | application code). Use this exception type in the `except` clause if you 878 | | want to handle all parse errors and serialization errors. 879 | | 880 | | If available, the `src_path` attribute will contain the KSY source path 881 | | pointing to the element where the error occurred. If it is not available, 882 | | `src_path` will be `None`. 883 | | """ | |_______^ 884 | 885 | def __init__(self, msg, src_path): | help: Insert single blank line D205 1 blank line required between summary line and description --> kaitaistruct.py:891:5 | 890 | class InvalidArgumentError(KaitaiStructError, ValueError): 891 | / """Indicates that an invalid argument value was received (like `ValueError`), 892 | | but used in places where this might indicate invalid user input and 893 | | therefore represents a parse error or serialization error. 894 | | """ | |_______^ 895 | 896 | def __init__(self, msg): | help: Insert single blank line D205 1 blank line required between summary line and description --> kaitaistruct.py:901:5 | 900 | class EndOfStreamError(KaitaiStructError, EOFError): 901 | / """Read or write beyond end of stream. Provides the `bytes_needed` (number 902 | | of bytes requested to read or write) and `bytes_available` (number of bytes 903 | | remaining in the stream) attributes. 904 | | """ | |_______^ 905 | 906 | def __init__(self, msg, bytes_needed, bytes_available): | help: Insert single blank line D205 1 blank line required between summary line and description --> kaitaistruct.py:913:5 | 912 | class NoTerminatorFoundError(EndOfStreamError): 913 | / """Special type of `EndOfStreamError` that occurs when end of stream is 914 | | reached before the required terminator is found. If you want to tolerate a 915 | | missing terminator, you can specify `eos-error: false` in the KSY 916 | | specification, in which case the end of stream will be considered a valid 917 | | end of field and this error will no longer be raised. 918 | | 919 | | The `term` attribute contains a `bytes` object with the searched terminator. 920 | | """ | |_______^ 921 | 922 | def __init__(self, term, bytes_available): | help: Insert single blank line D205 1 blank line required between summary line and description --> kaitaistruct.py:932:5 | 931 | class UndecidedEndiannessError(KaitaiStructError): 932 | / """Error that occurs when default endianness should be decided with 933 | | switch, but nothing matches (although using endianness expression 934 | | implies that there should be some positive result). 935 | | """ | |_______^ 936 | 937 | def __init__(self, src_path): | help: Insert single blank line D205 1 blank line required between summary line and description --> kaitaistruct.py:942:5 | 941 | class ValidationFailedError(KaitaiStructError): 942 | / """Common ancestor for all validation failures. Stores pointer to 943 | | KaitaiStream IO object which was involved in an error. 944 | | """ | |_______^ 945 | 946 | def __init__(self, msg, io, src_path): | help: Insert single blank line D205 1 blank line required between summary line and description --> kaitaistruct.py:957:5 | 956 | class ValidationNotEqualError(ValidationFailedError): 957 | / """Signals validation failure: we required "actual" value to be equal to 958 | | "expected", but it turned out that it's not. 959 | | """ | |_______^ 960 | 961 | def __init__(self, expected, actual, io, src_path): | help: Insert single blank line D205 1 blank line required between summary line and description --> kaitaistruct.py:970:5 | 969 | class ValidationLessThanError(ValidationFailedError): 970 | / """Signals validation failure: we required "actual" value to be 971 | | greater than or equal to "min", but it turned out that it's not. 972 | | """ | |_______^ 973 | 974 | def __init__(self, min_bound, actual, io, src_path): | help: Insert single blank line D205 1 blank line required between summary line and description --> kaitaistruct.py:983:5 | 982 | class ValidationGreaterThanError(ValidationFailedError): 983 | / """Signals validation failure: we required "actual" value to be 984 | | less than or equal to "max", but it turned out that it's not. 985 | | """ | |_______^ 986 | 987 | def __init__(self, max_bound, actual, io, src_path): | help: Insert single blank line D205 1 blank line required between summary line and description --> kaitaistruct.py:996:5 | 995 | class ValidationNotAnyOfError(ValidationFailedError): 996 | / """Signals validation failure: we required "actual" value to be 997 | | from the list, but it turned out that it's not. 998 | | """ | |_______^ 999 | 1000 | def __init__(self, actual, io, src_path): | help: Insert single blank line D205 1 blank line required between summary line and description --> kaitaistruct.py:1006:5 | 1005 | class ValidationNotInEnumError(ValidationFailedError): 1006 | / """Signals validation failure: we required "actual" value to be in 1007 | | the enum, but it turned out that it's not. 1008 | | """ | |_______^ 1009 | 1010 | def __init__(self, actual, io, src_path): | help: Insert single blank line D205 1 blank line required between summary line and description --> kaitaistruct.py:1016:5 | 1015 | class ValidationExprError(ValidationFailedError): 1016 | / """Signals validation failure: we required "actual" value to match 1017 | | the expression, but it turned out that it doesn't. 1018 | | """ | |_______^ 1019 | 1020 | def __init__(self, actual, io, src_path): | help: Insert single blank line D205 1 blank line required between summary line and description --> kaitaistruct.py:1036:5 | 1035 | class ConsistencyNotCheckedError(Exception): 1036 | / """Thrown when attempting to write an object whose consistency hasn't been 1037 | | checked since the last modification. 1038 | | """ | |_______^ 1039 | 1040 | def __init__(self): | help: Insert single blank line Found 19 errors. ```
1 parent 5230054 commit a071175

File tree

1 file changed

+105
-41
lines changed

1 file changed

+105
-41
lines changed

kaitaistruct.py

Lines changed: 105 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ def align_to_byte(self):
310310
self.bits = 0
311311

312312
def read_bits_int_be(self, n):
313+
"""Read an unsigned `n`-bit integer in big-endian parsing direction."""
313314
self.bits_write_mode = False
314315

315316
res = 0
@@ -338,7 +339,9 @@ def read_bits_int_be(self, n):
338339
return res
339340

340341
def read_bits_int(self, n):
341-
"""Deprecated and no longer used as of KSC 0.9. It is only available
342+
"""Read an unsigned `n`-bit integer in big-endian parsing direction.
343+
344+
Deprecated and no longer used as of KSC 0.9. It is only available
342345
for backwards compatibility and will be removed in the future.
343346
344347
KSC 0.9 and later uses `read_bits_int_be()` instead.
@@ -351,6 +354,7 @@ def read_bits_int(self, n):
351354
return self.read_bits_int_be(n)
352355

353356
def read_bits_int_le(self, n):
357+
"""Read an unsigned `n`-bit integer in little-endian parsing direction."""
354358
self.bits_write_mode = False
355359

356360
res = 0
@@ -467,7 +471,9 @@ def read_bytes_term_multi(self, term, include_term, consume_term, eos_error):
467471
r += c
468472

469473
def ensure_fixed_contents(self, expected):
470-
"""Deprecated and no longer used as of KSC 0.9. It is only available
474+
"""Check that the `expected` bytes follow in the stream.
475+
476+
Deprecated and no longer used as of KSC 0.9. It is only available
471477
for backwards compatibility and will be removed in the future.
472478
473479
KSC 0.9 and later explicitly raises `ValidationNotEqualError` from an
@@ -826,9 +832,16 @@ def byte_array_index_of(data, b):
826832

827833
@staticmethod
828834
def resolve_enum(enum_obj, value):
829-
"""Resolves value using enum: if the value is not found in the map,
830-
we'll just use literal value per se. Works around problem with Python
831-
enums throwing an exception when encountering unknown value.
835+
"""Convert an integer to the given enum if possible, otherwise return it back.
836+
837+
Enums in Python raise a `ValueError` exception when they encounter an unknown
838+
value. This method works around this issue so that attempting to convert an
839+
unknown integer value to an enum does not stop parsing by default.
840+
841+
If it is desired to stop parsing on unknown enum values, `valid/in-enum: true`
842+
can be used in the .ksy specification on a specific `seq` or `instances` enum
843+
field. This adds validation that the field contains one of the known (defined)
844+
enum values, otherwise a `ValidationNotInEnumError` exception is raised.
832845
"""
833846
try:
834847
return enum_obj(value)
@@ -871,11 +884,14 @@ def _write_back(self, parent):
871884

872885

873886
class KaitaiStructError(Exception):
874-
"""Common ancestor for all errors originating from correct Kaitai Struct
875-
usage (i.e. errors that indicate a problem with user input, not errors
876-
indicating incorrect usage that are not meant to be caught but fixed in the
877-
application code). Use this exception type in the `except` clause if you
878-
want to handle all parse errors and serialization errors.
887+
"""Common ancestor for all errors originating from correct Kaitai Struct usage.
888+
889+
Use this exception class in the `except` clause if you want to handle all
890+
parse errors and serialization errors.
891+
892+
"Correct usage" refers to errors that indicate a problem with user input,
893+
not errors indicating incorrect usage, which are not meant to be caught but
894+
fixed in the application code.
879895
880896
If available, the `src_path` attribute will contain the KSY source path
881897
pointing to the element where the error occurred. If it is not available,
@@ -888,19 +904,23 @@ def __init__(self, msg, src_path):
888904

889905

890906
class InvalidArgumentError(KaitaiStructError, ValueError):
891-
"""Indicates that an invalid argument value was received (like `ValueError`),
892-
but used in places where this might indicate invalid user input and
893-
therefore represents a parse error or serialization error.
907+
"""Raised when an invalid argument is encountered during parsing or serialization.
908+
909+
This is a subclass of `ValueError`, which is the standard built-in exception
910+
class for invalid arguments in Python. It is also a subclass of
911+
`KaitaiStructError`, as it indicates invalid user input and therefore
912+
represents a parsing or serialization error.
894913
"""
895914

896915
def __init__(self, msg):
897916
super().__init__(msg, None)
898917

899918

900919
class EndOfStreamError(KaitaiStructError, EOFError):
901-
"""Read or write beyond end of stream. Provides the `bytes_needed` (number
902-
of bytes requested to read or write) and `bytes_available` (number of bytes
903-
remaining in the stream) attributes.
920+
"""Raised when attempting to read or write beyond end of stream.
921+
922+
Provides the `bytes_needed` (number of bytes requested to read or write) and
923+
`bytes_available` (number of bytes remaining in the stream) attributes.
904924
"""
905925

906926
def __init__(self, msg, bytes_needed, bytes_available):
@@ -910,11 +930,17 @@ def __init__(self, msg, bytes_needed, bytes_available):
910930

911931

912932
class NoTerminatorFoundError(EndOfStreamError):
913-
"""Special type of `EndOfStreamError` that occurs when end of stream is
914-
reached before the required terminator is found. If you want to tolerate a
915-
missing terminator, you can specify `eos-error: false` in the KSY
916-
specification, in which case the end of stream will be considered a valid
917-
end of field and this error will no longer be raised.
933+
"""Raised when end of stream is reached before the required terminator is found.
934+
935+
This is a subclass of `EndOfStreamError` because it is a special type of
936+
reaching the end of the stream prematurely - see
937+
https://github.com/kaitai-io/kaitai_struct_python_runtime/issues/41 for an
938+
explanation.
939+
940+
If you want to tolerate a missing terminator, you can specify `eos-error: false`
941+
in the .ksy specification. Reaching the end of the stream will then be
942+
considered a valid end of the field, and this error will no longer be
943+
raised.
918944
919945
The `term` attribute contains a `bytes` object with the searched terminator.
920946
"""
@@ -929,18 +955,24 @@ def __init__(self, term, bytes_available):
929955

930956

931957
class UndecidedEndiannessError(KaitaiStructError):
932-
"""Error that occurs when default endianness should be decided with
933-
switch, but nothing matches (although using endianness expression
934-
implies that there should be some positive result).
958+
"""Raised when the calculated default endianness cannot be determined.
959+
960+
The default endianness should have been decided with a switch, but no case
961+
matches (although the endianness switch suggests that the result should be
962+
positive).
935963
"""
936964

937965
def __init__(self, src_path):
938966
super().__init__("unable to decide on endianness for a type", src_path)
939967

940968

941969
class ValidationFailedError(KaitaiStructError):
942-
"""Common ancestor for all validation failures. Stores pointer to
943-
KaitaiStream IO object which was involved in an error.
970+
"""Common ancestor for all validation failures.
971+
972+
The `io` attribute stores the `KaitaiStream` object representing the I/O
973+
stream associated with the error. Validation exceptions raised from
974+
`_check()` methods do not have an I/O stream available, so their `io` will
975+
be `None`.
944976
"""
945977

946978
def __init__(self, msg, io, src_path):
@@ -954,8 +986,16 @@ def __init__(self, msg, io, src_path):
954986

955987

956988
class ValidationNotEqualError(ValidationFailedError):
957-
"""Signals validation failure: we required "actual" value to be equal to
958-
"expected", but it turned out that it's not.
989+
"""Raised when validation specified using the `contents` or `valid/eq` keys fails.
990+
991+
This exception is the result of a validation failure: `actual` should have
992+
been equal to `expected`, but it was not.
993+
994+
The `expected` attribute contains the expected value specified in the
995+
`contents` or `valid/eq` key in .ksy specifications. Note that the syntax
996+
`valid/eq: <expression>` has a shorthand form `valid: <expression>`.
997+
998+
The `actual` attribute contains the actual value parsed from the stream.
959999
"""
9601000

9611001
def __init__(self, expected, actual, io, src_path):
@@ -967,8 +1007,15 @@ def __init__(self, expected, actual, io, src_path):
9671007

9681008

9691009
class ValidationLessThanError(ValidationFailedError):
970-
"""Signals validation failure: we required "actual" value to be
971-
greater than or equal to "min", but it turned out that it's not.
1010+
"""Raised when validation specified using the `valid/min` key fails.
1011+
1012+
This exception is the result of a validation failure: `actual` should have
1013+
been greater than or equal to `min`, but it was not.
1014+
1015+
The `min` attribute contains the minimum value specified in the `valid/min`
1016+
key in .ksy specifications.
1017+
1018+
The `actual` attribute contains the actual value parsed from the stream.
9721019
"""
9731020

9741021
def __init__(self, min_bound, actual, io, src_path):
@@ -980,8 +1027,15 @@ def __init__(self, min_bound, actual, io, src_path):
9801027

9811028

9821029
class ValidationGreaterThanError(ValidationFailedError):
983-
"""Signals validation failure: we required "actual" value to be
984-
less than or equal to "max", but it turned out that it's not.
1030+
"""Raised when validation specified using the `valid/max` key fails.
1031+
1032+
This exception is the result of a validation failure: `actual` should have
1033+
been less than or equal to `max`, but it was not.
1034+
1035+
The `max` attribute contains the maximum value specified in the `valid/max`
1036+
key in .ksy specifications.
1037+
1038+
The `actual` attribute contains the actual value parsed from the stream.
9851039
"""
9861040

9871041
def __init__(self, max_bound, actual, io, src_path):
@@ -993,8 +1047,12 @@ def __init__(self, max_bound, actual, io, src_path):
9931047

9941048

9951049
class ValidationNotAnyOfError(ValidationFailedError):
996-
"""Signals validation failure: we required "actual" value to be
997-
from the list, but it turned out that it's not.
1050+
"""Raised when validation specified using the `valid/any-of` key fails.
1051+
1052+
This exception is the result of a validation failure: `actual` should have
1053+
been any of the values listed in the `valid/any-of` key, but it was not.
1054+
1055+
The `actual` attribute contains the actual value parsed from the stream.
9981056
"""
9991057

10001058
def __init__(self, actual, io, src_path):
@@ -1003,8 +1061,12 @@ def __init__(self, actual, io, src_path):
10031061

10041062

10051063
class ValidationNotInEnumError(ValidationFailedError):
1006-
"""Signals validation failure: we required "actual" value to be in
1007-
the enum, but it turned out that it's not.
1064+
"""Raised when validation specified using the `valid/in-enum` key fails.
1065+
1066+
This exception is the result of a validation failure: `actual` should have
1067+
been a known value defined in the enum, but it was not.
1068+
1069+
The `actual` attribute contains the actual value parsed from the stream.
10081070
"""
10091071

10101072
def __init__(self, actual, io, src_path):
@@ -1013,8 +1075,12 @@ def __init__(self, actual, io, src_path):
10131075

10141076

10151077
class ValidationExprError(ValidationFailedError):
1016-
"""Signals validation failure: we required "actual" value to match
1017-
the expression, but it turned out that it doesn't.
1078+
"""Raised when validation specified using the `valid/expr` key fails.
1079+
1080+
This exception is the result of a validation failure: `actual` should have
1081+
satisfied the validation expression in the `valid/expr` key, but it did not.
1082+
1083+
The `actual` attribute contains the actual value parsed from the stream.
10181084
"""
10191085

10201086
def __init__(self, actual, io, src_path):
@@ -1033,9 +1099,7 @@ def __init__(self, attr_id, expected, actual):
10331099

10341100

10351101
class ConsistencyNotCheckedError(Exception):
1036-
"""Thrown when attempting to write an object whose consistency hasn't been
1037-
checked since the last modification.
1038-
"""
1102+
"""Raised when attempting to write an object with unchecked consistency."""
10391103

10401104
def __init__(self):
10411105
super().__init__(

0 commit comments

Comments
 (0)