code: def _make_valid_if_necessary(shape):
      
      if shape.type in ('Polygon', 'MultiPolygon') and not shape.is_valid:
          shape = shape.buffer(0)
  
          # return value from buffer is usually valid, but it's
          # not clear from the docs whether this is guaranteed,
          # so return None if not.
          if not shape.is_valid:
              return None
  
      return shape
docs: attempt to correct invalid shapes if necessary
  
      After simplification, even when preserving topology, invalid
      shapes can be returned. This appears to only occur with polygon
      types. As an optimization, we only check if the polygon types are
      valid.