<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def with_fragment(self, fragment):
 """Return a new URL with fragment replaced. Autoencode fragment if needed. Clear fragment to default if None is passed. """

        # N.B. doesn't cleanup query/fragment
        if fragment is None:
            fragment = ""
        elif not isinstance(fragment, str):
            raise TypeError("Invalid fragment type")
        return URL(
            self._val._replace(fragment=self._FRAGMENT_QUOTER(fragment)), encoded=True
        )