# Buggy code
def __init__(self, segments, pos_marker=None):
    if len(segments) == 0:
        raise RuntimeError(
            "Setting {0} with a zero length segment set. This shouldn't happen.".format(
                self.__class__
            )
        )

    if hasattr(segments, "matched_segments"):
        # Safely extract segments from a match
        self.segments = segments.matched_segments
    elif isinstance(segments, tuple):
        self.segments = segments
    elif isinstance(segments, list):
        self.segments = tuple(segments)
    else:
        raise TypeError(
            "Unexpected type passed to BaseSegment: {0}".format(type(segments))
        )

    # Check elements of segments:
    self.validate_segments()

    if pos_marker:
        self.pos_marker = pos_marker
    else:
        # If no pos given, it's the pos of the first segment
        # Work out if we're dealing with a match result...
        if hasattr(segments, "initial_match_pos_marker"):
            self.pos_marker = segments.initial_match_pos_marker()
        elif isinstance(segments, (tuple, list)):
            self.pos_marker = segments[0].pos_marker
        else:
            raise TypeError(
                "Unexpected type passed to BaseSegment: {0}".format(type(segments))
            )


# Traceback (TypeError)
$ sqlfluff fix --rules L001 forecastservice\sql\get_phasing_v2.sql
==== finding violations ====
== [forecastservice\sql\get_phasing_v2.sql] FAIL
L:   0 | P:   0 | ???? | Failure in Jinja templating: 'first_mon' is undefined. Have you configured your variables?
L:   7 | P:   1 | ???? | Unable to lex characters: ''{% if regi'...'
==== fixing violations ====
2 violations found
Are you sure you wish to attempt to fix these? [Y/n] ...
Attempting fixes...
Persisting Changes...
Traceback (most recent call last):
File "C:\Users\alan\dev\tails-analytics-forecastservice\env\Scripts\sqlfluff-script.py", line 11, in <module>
load_entry_point('sqlfluff==0.2.4', 'console_scripts', 'sqlfluff')()
File "c:\users\alan\dev\tails-analytics-forecastservice\env\lib\site-packages\click\core.py", line 764, in __call__
return self.main(*args, **kwargs)
File "c:\users\alan\dev\tails-analytics-forecastservice\env\lib\site-packages\click\core.py", line 717, in main
rv = self.invoke(ctx)
File "c:\users\alan\dev\tails-analytics-forecastservice\env\lib\site-packages\click\core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "c:\users\alan\dev\tails-analytics-forecastservice\env\lib\site-packages\click\core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "c:\users\alan\dev\tails-analytics-forecastservice\env\lib\site-packages\click\core.py", line 555, in invoke
return callback(*args, **kwargs)
File "c:\users\alan\dev\tails-analytics-forecastservice\env\lib\site-packages\sqlfluff\cli\commands.py", line 197, in fix
result.persist_changes(verbosity=verbose)
File "c:\users\alan\dev\tails-analytics-forecastservice\env\lib\site-packages\sqlfluff\linter.py", line 327, in persist_changes
return self.combine_dicts(*[path.persist_changes(verbosity=verbosity) for path in self.paths])
File "c:\users\alan\dev\tails-analytics-forecastservice\env\lib\site-packages\sqlfluff\linter.py", line 327, in <listcomp>
return self.combine_dicts(*[path.persist_changes(verbosity=verbosity) for path in self.paths])
File "c:\users\alan\dev\tails-analytics-forecastservice\env\lib\site-packages\sqlfluff\linter.py", line 253, in persist_changes
return {file.path: file.persist_tree(verbosity=verbosity) for file in self.files}
File "c:\users\alan\dev\tails-analytics-forecastservice\env\lib\site-packages\sqlfluff\linter.py", line 253, in <dictcomp>
return {file.path: file.persist_tree(verbosity=verbosity) for file in self.files}
File "c:\users\alan\dev\tails-analytics-forecastservice\env\lib\site-packages\sqlfluff\linter.py", line 61, in persist_tree
diff_templ_codes = diff_templ.get_opcodes()
File "C:\Users\alan\AppData\Local\Programs\Python\Python36\lib\difflib.py", line 550, in get_opcodes
for ai, bj, size in self.get_matching_blocks():
File "C:\Users\alan\AppData\Local\Programs\Python\Python36\lib\difflib.py", line 467, in get_matching_blocks
la, lb = len(self.a), len(self.b)
TypeError: object of type 'NoneType' has no len()

# Fixed code
def __init__(self, segments, pos_marker=None, validate=True):
    if len(segments) == 0:
        raise RuntimeError(
            "Setting {0} with a zero length segment set. This shouldn't happen.".format(
                self.__class__
            )
        )

    if hasattr(segments, "matched_segments"):
        # Safely extract segments from a match
        self.segments = segments.matched_segments
    elif isinstance(segments, tuple):
        self.segments = segments
    elif isinstance(segments, list):
        self.segments = tuple(segments)
    else:
        raise TypeError(
            "Unexpected type passed to BaseSegment: {0}".format(type(segments))
        )

    # Check elements of segments:
    self.validate_segments(validate=validate)

    if pos_marker:
        self.pos_marker = pos_marker
    else:
        # If no pos given, it's the pos of the first segment
        # Work out if we're dealing with a match result...
        if hasattr(segments, "initial_match_pos_marker"):
            self.pos_marker = segments.initial_match_pos_marker()
        elif isinstance(segments, (tuple, list)):
            self.pos_marker = segments[0].pos_marker
        else:
            raise TypeError(
                "Unexpected type passed to BaseSegment: {0}".format(type(segments))
            )